LoadError:无法加载此类文件 - gemname / base

时间:2013-09-02 10:46:49

标签: ruby-on-rails ruby rubygems ruby-2.0

我正在开发一个简单的红宝石宝石,其中我需要额外的课程。 以下是我的gemname / lib目录的文件:

lib/
   gemname/
          base.rb
          version.rb
   gemname.rb

我已将声明require "gemname/base"包含在我的gemname.rb中。

在本地构建和安装gem并转到irbrequire gemname之后,我收到以下错误:

LoadError: cannot load such file -- ./gemname/base
    from /home/swaroop/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:51:in `require'
    from /home/swaroop/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:51:in `require'
    from /home/swaroop/.rvm/gems/ruby-2.0.0-p0/gems/gemname-0.0.6/lib/gemname.rb:2:in `<top (required)>'
    from /home/swaroop/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:116:in `require'
    from /home/swaroop/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:116:in `rescue in require'
    from /home/swaroop/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:122:in `require'
    from (irb):1
    from /home/swaroop/.rvm/rubies/ruby-2.0.0-p0/bin/irb:13:in `<main>'

我哪里错了?

2 个答案:

答案 0 :(得分:5)

您的gemname.gemspec文件应该包含这样的内容。

spec.files = `git ls-files -z`.split("\x0")

您收到错误消息,因为您没有使用git版本文件。如果您在使用rake install之前运行git,那么如果您执行

irb
require "gemname"

这会引发错误。

这就是原因。看看它的安装路径。

gem list -d gemname  # this should return something like this

gemname (0.0.1)
    Author: your name
    License: MIT
    Installed at: /Users/your_name/.rbenv/versions/2.0.0/lib/ruby/gems/2.0.0

打开位置并找到您的gem文件夹。你应该在那里看到nothing

现在,将您的文件添加到git并至少提交一次

git init
git add .
git commit -m "init"

然后执行rake install并要求它不应该引发错误。

irb
require "gemname" # no error, and you should see files in this gem's install folder.

答案 1 :(得分:2)

lib/gemname.rb

require 'gemname/base.rb'

希望在gemname.specification文件中有

s.files = Dir["README.md","Gemfile","Rakefile", "spec/*", "lib/**/*"]

例如,看看我的宝石

https://github.com/rajcybage/google_book

http://rubygems.org/gems/google_book