开发者

Gem found in irb, not in Ruby

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

require 'rubygems'
require 'mygem'

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

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.

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

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

$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 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 shows

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

Gem.path in irb points to

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

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.

In irb, do

require 'rubygems'
require 'mygem'
puts $:

and then, in ruby, do

require 'rubygems'
puts $:

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

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

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


You may try to add gem 'mygem' before the require, but that should not be necessary.


You will have to add gem install mygem in your Gamefile and then run bundle install command. Your application will work correctly after doing this.


I had a similar problem. The solution I found eventually was to setup rvm (ruby version manager) on my system and use it to setup a new ruby environment. it also makes it easy to switch between ruby versions of sets of gems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜