Rails 3 库直到需要才加载 [英] Rails 3 library not loading until require
问题描述
我正在尝试在 rails 3 中加载 Tokbox SDK.我已将该库放在我的/lib 目录中,因此目前我的目录结构如下所示:
I'm trying to load the Tokbox SDK in rails 3. I've placed the library in my /lib directory, so currently my directory structure looks like so:
/lib
opentok.rb
/OpenTok
Exceptions.rb
OpenTokSDK.rb
Session.rb
/lib
opentok.rb
/OpenTok
Exceptions.rb
OpenTokSDK.rb
Session.rb
我正在使用 application.rb 中的以下内容加载/lib 目录中的所有文件:
I'm loading all files in the /lib directory using the following in application.rb:
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
我在/lib 目录中的其他文件自动加载就好了,但是在我添加 require 'OpenTok'
之前,这个库不会加载:
Other files I have in the /lib directory are auto-loading just fine, but this library does not load until I add a require 'OpenTok'
:
ruby-1.9.2-p0 > OpenTok
NameError: uninitialized constant OpenTok
ruby-1.9.2-p0 > OpenTok::OpenTokSDK
NameError: uninitialized constant OpenTok
ruby-1.9.2-p0 > require 'OpenTok'
=> ["OpenTok"]
ruby-1.9.2-p0 > OpenTok
=> OpenTok
ruby-1.9.2-p0 > OpenTok::OpenTokSDK
=> OpenTok::OpenTokSDK
在 Rails 3 中加载库的正确方法是什么?
What is the correct way to load the library in Rails 3?
推荐答案
自动加载器会将常量设置为蛇形大小写,因此OpenTok"将使自动加载器查找open_tok.rb",而不是opentok.rb".尝试重命名 lib/opentok.rb,它应该可以正常工作.
The autoloader will snake case the constant, so "OpenTok" would make the autoloader look for "open_tok.rb", not "opentok.rb". Try renaming lib/opentok.rb and it should work fine.
这篇关于Rails 3 库直到需要才加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!