为什么自动加载方法在我的库中找不到该文件?

发布于 2024-11-03 21:44:41 字数 3329 浏览 0 评论 0原文

我正在将我的库打包成 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热鲨 2024-11-10 21:44:41

我发现问题是我制作了错误的 gemspec。

spec.files = Dir["{lib/*,spec/*}"] + %w{README}

它没有将文件打包到“lib/renren_api/”下。
像这样改变一下就可以解决这个问题了。

spec.files = Dir["{lib/**/*,spec/*}"] + %w{README}

I find the problem is I made a wrong gemspec.

spec.files = Dir["{lib/*,spec/*}"] + %w{README}

It didn't package the file under "lib/renren_api/".
To change it like this will solve this problem.

spec.files = Dir["{lib/**/*,spec/*}"] + %w{README}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文