Rails 3库直到需要时才加载 [英] Rails 3 library not loading until require
问题描述
我试图将Tokbox SDK加载到rails 3中.我已将该库放置在/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屋!