我正在将我的图书馆打包成宝石。这是项目的结构。
|~lib/
| |~renren_api/
| | |-authentication.rb
| | |-http_adapter.rb
| | \-signature_calculator.rb
| \-renren_api.rb
|+spec/
|-README
\-renren-api.gemspec
我写“lib / renren-api.rb”如下。 Rack激励我。
module RenrenAPI
VERSION = [0, 3, 1]
def self.version
VERSION * "."
end
autoload :Authentication, "renren_api/authentication"
autoload :SignatureCalculator, "renren_api/signature_calculator"
autoload :HTTPAdapter, "renren_api/http_adapter"
end
为什么自动加载方法找不到所需的文件,但Rack可以?
ruby-1.9.2-head :001 > require "renren_api"
=> true
ruby-1.9.2-head :002 > RenrenAPI::Authentication
LoadError: no such file to load -- renren_api/authentication
from (irb):2
from /Users/siegfried/.rvm/rubies/ruby-1.9.2-head/bin/irb:16:in `<main>'
ruby-1.9.2-head :003 > RenrenAPI::HTTPAdapter
LoadError: no such file to load -- renren_api/http_adapter
from (irb):3
from /Users/siegfried/.rvm/rubies/ruby-1.9.2-head/bin/irb:16:in `<main>'
# The following split into newlines to make more readable
ruby-1.9.2-head :004 > $:
=> ["/Users/siegfried/.rvm/gems/ruby-1.9.2-head/gems/renren-api-0.3.1/lib",
"/Users/siegfried/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1",
"/Users/siegfried/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.6.0",
"/Users/siegfried/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby",
"/Users/siegfried/.rvm/rubies/ruby-1.9.2-head/lib/ruby/vendor_ruby/1.9.1",
"/Users/siegfried/.rvm/rubies/ruby-1.9.2-head/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.6.0",
"/Users/siegfried/.rvm/rubies/ruby-1.9.2-head/lib/ruby/vendor_ruby",
"/Users/siegfried/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1",
"/Users/siegfried/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/x86_64-darwin10.6.0"]
我的gemspec文件如下。
Gem::Specification.new do |spec|
spec.name = "renren-api"
spec.version = "0.3.1"
spec.summary = "a library to request Renren's API"
spec.description = <<-EOF
renren-api provides capability to request the service of Renren Social Network.
EOF
spec.files = Dir["{lib/*,spec/*}"] + %w{README}
spec.require_path = "lib"
spec.extra_rdoc_files = %w{README}
spec.test_files = Dir["spec/*_spec.rb"]
spec.author = "Lei, Zhi-Qiang"
spec.email = "#my email"
spec.homepage = "https://github.com/siegfried/renren-api"
end
当需要“renren_api / authentication”时。
ruby-1.9.2-head :001 > require "renren_api/authentication"
LoadError: no such file to load -- renren_api/authentication
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from (irb):1
from /Users/siegfried/.rvm/rubies/ruby-1.9.2-head/bin/irb:16:in `<main>'
添加“.rb”无济于事。
ruby-1.9.2-head :001 > require "renren_api"
=> true
ruby-1.9.2-head :002 > RenrenAPI::Authentication
LoadError: no such file to load -- renren_api/authentication.rb
from (irb):2
from /Users/siegfried/.rvm/rubies/ruby-1.9.2-head/bin/irb:16:in `<main>'
答案 0 :(得分:0)
我发现问题是我做错了gemspec。
spec.files = Dir["{lib/*,spec/*}"] + %w{README}
它没有将文件打包在“lib / renren_api /”下。 像这样改变它将解决这个问题。
spec.files = Dir["{lib/**/*,spec/*}"] + %w{README}