宝石在 irb 中发现,而不是在 Ruby 中 [英] Gem found in irb, not in Ruby

查看:21
本文介绍了宝石在 irb 中发现,而不是在 Ruby 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了一些 gem,我正在尝试在 Ruby 应用程序中使用它们:

I have some gems installed, and I'm trying to use them in a Ruby app:

require 'rubygems'
require 'mygem'

但是,当我运行应用程序时,我收到此错误:<internal:lib/rubygems/custom_require>:29:inrequire': no such file to load -- mygem (LoadError)`

When I run the app, though, I get this error: <internal:lib/rubygems/custom_require>:29:inrequire': no such file to load -- mygem (LoadError)`

但是,如果我尝试在 irb 中要求 gem(确保首先 `require 'rubygems'),它可以正常工作.我应该做些什么?我尝试用谷歌搜索这个问题,但不明白.

But if I try requiring the gem inside irb (making sure to `require 'rubygems' first), it works fine. What am I supposed to do? I tried googling for this problem, but didn't understand.

rubygemirb 上运行 which 表明它们都在 /opt/local/bin/,即,

Running a which on ruby, gem, and irb shows that they're all in /opt/local/bin/, i.e.,

> which ruby
/opt/local/bin/ruby
> which gem
/opt/local/bin/gem
> which irb
/opt/local/bin/irb

更新以回答提出的问题(是的,irb 和 ruby​​ 指向不同的文件夹):

Update to answer the questions posed (yep, irb and ruby are pointing to different folders):

$LOAD_PATH$: 都包含似乎指向 ruby​​ 1.8 文件夹:

$LOAD_PATH and $: in irb both contain seem to be pointing to ruby 1.8 folders:

/opt/local/lib/ruby/site_ruby/1.8
/opt/local/lib/ruby/site_ruby/1.8/i686-darwin10
/opt/local/lib/ruby/site_ruby
/opt/local/lib/ruby/vendor_ruby/1.8
/opt/local/lib/ruby/vendor_ruby/1.8/i686-darwin10
/opt/local/lib/ruby/vendor_ruby
/opt/local/lib/ruby/1.8
/opt/local/lib/ruby/1.8/i686-darwin10
.

$: in ruby​​ 指向 ruby​​ 1.9.1 文件夹:

$: in ruby points to ruby 1.9.1 folders:

/usr/local/lib/ruby/site_ruby/1.9.1
/usr/local/lib/ruby/site_ruby/1.9.1/i386-darwin9.8.0
/usr/local/lib/ruby/site_ruby
/usr/local/lib/ruby/vendor_ruby/1.9.1
/usr/local/lib/ruby/vendor_ruby/1.9.1/i386-darwin9.8.0
/usr/local/lib/ruby/vendor_ruby
/usr/local/lib/ruby/1.9.1
/usr/local/lib/ruby/1.9.1/i386-darwin9.8.0

gem env 展示

RubyGems Environment:
  - RUBYGEMS VERSION: 1.4.1
  - RUBY VERSION: 1.8.7 (2010-12-23 patchlevel 330) [i686-darwin10]
  - INSTALLATION DIRECTORY: /opt/local/lib/ruby/gems/1.8
  - RUBY EXECUTABLE: /opt/local/bin/ruby
  - EXECUTABLE DIRECTORY: /opt/local/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-darwin-10
  - GEM PATHS:
     - /opt/local/lib/ruby/gems/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
     - "gempath" => ["/opt/local/lib/ruby/gems/1.8"]
     - :sources => ["http://rubygems.org/", "http://gems.github.com", "http://gems.github.com"]
  - REMOTE SOURCES:
     - http://rubygems.org/
     - http://gems.github.com
     - http://gems.github.com

irb中的

Gem.path指向

/Users/grautur/.gem/ruby/1.8
/usr/local/lib/ruby/gems/1.8

ruby 中的

Gem.path 指向

Gem.path in ruby points to

/Users/grautur/.gem/ruby/1.9.1
/usr/local/lib/ruby/gems/1.9.1

推荐答案

我不知道发生了什么.但是,以下可能会有所帮助.

I'm not sure what's going on. However, the following may help.

在irb中,做

require 'rubygems'
require 'mygem'
puts $:

然后,在 ruby​​ 中,做

and then, in ruby, do

require 'rubygems'
puts $:

如果你还没有解决,请告诉我们你得到了什么.

and show us what you get if you haven't worked it out.

同样在命令行打印出gem env的结果.

also print out the results of doing gem env on the command line.

编辑 2: 看看如果在 irb 和 ruby​​ 中都需要 ruby​​gems 之后键入 puts Gem.path 会发生什么.请参阅感谢 Matt 描述 Rubygems

Edit 2: See what happens if you type in puts Gem.path after you've required rubygems in both irb and ruby. See thanks to Matt for describing Rubygems

这篇关于宝石在 irb 中发现,而不是在 Ruby 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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