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 - o开发者_如何学JAVAmniauth/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?
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:
Gem::Specification.new do |spec|
# ...
spec.require_paths = [ 'lib' ]
# ...
end
PS: Just to solve the initial problem: You probably meant to add the following to $LOAD_PATH
: File.expand_path(File.dirname __FILE__)
.
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).
精彩评论