为什么自动加载方法在我的库中找不到该文件?
我正在将我的库打包成 gem。这是该项目的结构。
|~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
为什么autoload方法找不到需要的文件,而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>'
I'm packaging my library into gem. This is the structure of the project.
|~lib/
| |~renren_api/
| | |-authentication.rb
| | |-http_adapter.rb
| | \-signature_calculator.rb
| \-renren_api.rb
|+spec/
|-README
\-renren-api.gemspec
I write the "lib/renren-api.rb" as follow. Rack inspire me.
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
Why the autoload method cannot find the required file, but Rack's can?
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"]
My gemspec file is as follow.
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
When require "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>'
Add ".rb" will not help.
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>'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现问题是我制作了错误的 gemspec。
它没有将文件打包到“lib/renren_api/”下。
像这样改变一下就可以解决这个问题了。
I find the problem is I made a wrong gemspec.
It didn't package the file under "lib/renren_api/".
To change it like this will solve this problem.