Ruby 的自动加载在 1.8.7 或 Ruby Enterprise 中不起作用?
我已经编写了一个 gem,并在一个文件中这样做是为了自动加载我的主要 gem 逻辑:
$:.push File.expand_path('lib', __FILE__)
require "oa-casport/version"
require 'omniauth/core'
module OmniAuth
module Strategies
autoload :Casport, 'omniauth/strategies/casport'
end
end
对于 Ruby 版本 1.8.7 和 ree,它打印出“没有要加载的文件 -omniauth/strategies/casport”
但在 1.9.2 版本上它不会打印出此消息。调用自动加载的位置是否有问题?
gem 的存储库位于 https://github.com/stevenhaddox/oa-casport
编辑: 我的 gem 适用于 Rails 2 和 3,无论版本如何,但在使用 Ruby/REE 1.8.7 时不适用于 Sinatra。有什么想法吗?
I've written a gem and within a file I am doing this to autoload my main gem logic:
$:.push File.expand_path('lib', __FILE__)
require "oa-casport/version"
require 'omniauth/core'
module OmniAuth
module Strategies
autoload :Casport, 'omniauth/strategies/casport'
end
end
For Ruby versions 1.8.7 and ree, it prints out "no such file to load - omniauth/strategies/casport'
But it doesn't print out this message on version 1.9.2. Is there something off with the location of calling autoload?
The repo for the gem is located at https://github.com/stevenhaddox/oa-casport
EDIT: My gem works for Rails 2 and 3 regardless of version, but doesn't work on Sinatra when using Ruby/REE 1.8.7. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您向
$LOAD_PATH
添加了错误的路径。File.expand_path('lib', __FILE__)
将计算为${GEM_PATH}/lib/oa-casport.rb/lib
,这显然不存在。相反,请在 gemspec 中指定路径:
PS:只是为了解决最初的问题:您可能打算将以下内容添加到
$LOAD_PATH
中:File.expand_path(File.dirname __FILE__).
You're adding a wrong path to
$LOAD_PATH
.File.expand_path('lib', __FILE__)
will evaluate to${GEM_PATH}/lib/oa-casport.rb/lib
which obviously doesn't exist.Instead, specify your paths in your gemspec:
PS: Just to solve the initial problem: You probably meant to add the following to
$LOAD_PATH
:File.expand_path(File.dirname __FILE__)
.我检查了代码,看起来它可以在带有 Ruby 1.8.7 和 1.9.2 的 Rails 2 或 Rails 3 上正常加载,但只有在 Ruby 1.8.7 下的 Sinatra 出现问题(在 1.9.2 上加载正常)。
我仍然不确定为什么会出现这种差异,但当有机会时我会继续研究它。它在上面的大多数环境中工作的事实似乎告诉我 $:.push 行并没有真正引起任何问题(但它可能没有必要,因为您使用 git 来打包已经在 .宝石规格)。
I checked out the code and it looks like it loads fine with Rails 2 or Rails 3 with Ruby 1.8.7 and 1.9.2, but is only having issues with Sinatra under Ruby 1.8.7 (loads fine with 1.9.2).
I'm still not sure why the discrepancy, but I'll keep looking into it when I get a chance. The fact that it works in most of the environments above seems to tell me the $:.push line isn't really causing any problems (but it may not be necessary since you're using git to package your gem files already in the .gemspec).