创建红宝石时产生LoadError [英] LoadError when creating ruby gem

查看:85
本文介绍了创建红宝石时产生LoadError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  riverbattle 
├──lib

我试图创建我的第一个gem,这里是它的结构:
│├──riverbattle
││├──base.rb
││├──colorful.rb
││├──computer.rb
│ │├──constants.rb
││├──exit_error.rb
││├──field.rb
││├──game.rb
││├ ──human.rb
││├──invalid_move_error.rb
││├──move.rb
││├──player.rb
││├── version.rb
││└──victory_error.rb
│└──riverbattle.rb
└──riverbattle.gemspec

当我在 rivarbattle / lib / riverbattle 目录中运行命令 ruby base.rb ,一切都开始工作。



文件 base.rb 在这里:

  require'./游戏'
game = Game.new.start

但是当我尝试从使用命令 ruby​​ riverbattle.rb lib 目录,它会失败并显示以下错误:


/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54 : require':无法加载这个文件 - riverbattle / base
(LoadError)from
/home/denis/.rvm/rubies/ruby-2.2.1/lib/ ruby / site_ruby / 2.2.0 / ruby​​gems / core_ext / kernel_require.rb:54:
require'from riverbattle.rb:1:in''



$ b

文件 lib / riverbattle.rb 看起来像这样:

  require'riverbattle / base'

尽管负载失败我试图用以下.gemspec文件构建gem:

 # -  *  - 编码:utf-8  -  *  -  
lib = File.expand_path('../ lib',__FILE__)
$ LOAD_PATH.unshift(lib)除非$ LOAD_PATH.include?(lib)
需要riverbattle /版本

Gem :: Specification.new do | s |
s.name ='riverbattle'
s.date ='2015-07-04'
s.summary ='享受游戏!'
s.description ='A小型简单游戏称为Riverbattle'
s.platform = Gem :: Platform :: RUBY
s.version ='0.1.0'
s.authors = [Denis Yakovenko]
s.email = [yakovenko.denis.a@gmail.com]
s.require_paths = [lib]
s.license =MIT
end

成功构建后,我去了irb并尝试了 require'riverbattle'并得到错误:


LoadError:无法加载此文件 - riverbattle



from
/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require'from
/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in
要求 from(irb):1 from
/home/denis/.rvm/rubies/ruby-2.2.1/bin/irb:11:in`'







1)我在做什么错误 require ,来自不同目录的文件不能装? (在查看stackoverflow问题后,我发现这是$ LOAD_PATH的问题,但我找不到如何修复代码。顺便说一句,需要 LoadErrors发生2)当我尝试使用 require_relative ,有时它会有帮助)

2)为了让irb中的宝石出现问题,由于 require 的最初问题引发错误,对吧?或者别的什么是错的?


$ b $ 3)如果我想将测试文件夹添加到gem,我可以添加文件夹 tests 到根目录并复制所有rspec文件?

更新



当我尝试运行 RUBYLIB =。 ruby riverbattle.rb 在终端中的 lib 文件夹中,它给了我:



< blockquote>

RUBYLIB =。 ruby riverbattle.rb
/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require':无法加载这样的文件 -
中的./field(LoadError)/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext /kernel_require.rb:54:in
require'from
/home/denis/WEB/Bursa/riverbattle/lib/riverbattle/game.rb:1:in
< top(required)>'from
/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require。 rb:54:
require'from
/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require .rb:54: require'from
/home/denis/WEB/Bursa/riverbattle/lib/riverbattle/base.rb:1:in
'from
/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require'from
/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/sit e_ruby / 2.2.0 / ruby​​gems / core_ext / kernel_require.rb:54:在
require'from riverbattle.rb:1:in''



解决方案

1。
#require 尝试加载绝对路径,如果失败,则从 $:加载到添加你的当前目录像这样运行脚本(bash,否则使用适当的方法为你的shell定义环境变量):

  RUBYLIB =  ruby riverbattle.rb 

在您的 base.rb 要求如下:

 要求'riverbattle / game'

2。
请参阅#UPD部分



3。
为什么不呢? (这是什么特定问题



#UPD



第二个问题是因为你错过
$ b $ b

  s.files = Dir ['lib / *。rb'] + Dir ['lib / riverbattle / *。rb'] 



#UPD2



总结这里正在为您的宝石设置工作。


I'm trying to create my first gem and here's its structure:

riverbattle
├── lib
│   ├── riverbattle
│   │   ├── base.rb
│   │   ├── colorful.rb
│   │   ├── computer.rb
│   │   ├── constants.rb
│   │   ├── exit_error.rb
│   │   ├── field.rb
│   │   ├── game.rb
│   │   ├── human.rb
│   │   ├── invalid_move_error.rb
│   │   ├── move.rb
│   │   ├── player.rb
│   │   ├── version.rb
│   │   └── victory_error.rb
│   └── riverbattle.rb
└── riverbattle.gemspec

When I am in the directory rivarbattle/lib/riverbattle and I run the command ruby base.rb, everything starts working as it should be.

The file base.rb is here:

require './game'
game = Game.new.start

But when I try to run it from the lib directory using command ruby riverbattle.rb, it fails with the following error:

/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require': cannot load such file -- riverbattle/base (LoadError) from /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from riverbattle.rb:1:in `'

The file lib/riverbattle.rb look like this:

require 'riverbattle/base'

Despite the fact that the load fails I tried to build the gem with the following .gemspec file:

# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "riverbattle/version"

Gem::Specification.new do |s|
    s.name          = 'riverbattle'
    s.date          = '2015-07-04'
    s.summary       = 'Enjoy the game!'
    s.description   = 'A small simple game called the Riverbattle'
    s.platform      = Gem::Platform::RUBY
    s.version       = '0.1.0'
    s.authors       = ["Denis Yakovenko"]
    s.email         = ["yakovenko.denis.a@gmail.com"]
    s.require_paths = ["lib"]
    s.license       = "MIT"
end

After the successful build, I went to irb and tried require 'riverbattle' and got the error:

LoadError: cannot load such file -- riverbattle

from /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require' from /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from (irb):1 from /home/denis/.rvm/rubies/ruby-2.2.1/bin/irb:11:in `'


1) What am I doing wrong with require that the files from different directories cannot be loaded? (After looking through stackoverflow questions I figured out that this is some problem with $LOAD_PATH, but I couldn't find out how to fix the code. BTW, such require LoadErrors happen quite often for some reason and then I try to use require_relative and sometimes it helps)

2) When I tried to lauch the gem in irb, the error was raised because of those initial problems with require, right? Or something else is wrong?

3) If I want to add tests folder to the gem, can I just add the folder tests to the root directory and copy all the rspec files there?

UPDATE

When I tried to run RUBYLIB="." ruby riverbattle.rb in the terminal in the lib folder, it gave me this:

RUBYLIB="." ruby riverbattle.rb /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require': cannot load such file -- ./field (LoadError) from /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from /home/denis/WEB/Bursa/riverbattle/lib/riverbattle/game.rb:1:in <top (required)>' from /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require' from /home/denis/WEB/Bursa/riverbattle/lib/riverbattle/base.rb:1:in ' from /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require' from /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from riverbattle.rb:1:in `'

解决方案

1. #require tries to load an absolute path, and if it fails it loads from $:, to add your current directory run the script like this (bash, otherwise use apropriate method to define environment variables for your shell):

RUBYLIB="." ruby riverbattle.rb

In your base.rb require as follows:

require 'riverbattle/game'

2. see #UPD section

3. why wouldn't you? (what's your specific problem with this?)

#UPD

The 2nd issue is because you miss #files in your gem specification, no files are included in your gem file, add something like this:

s.files = Dir['lib/*.rb'] + Dir['lib/riverbattle/*.rb']

#UPD2

Just to summarize here is working setup for your gem.

这篇关于创建红宝石时产生LoadError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆