4

I'm trying to use a gem i just installed (via sudo gem install excelsior) like so

require 'rubygems'

require 'excelsior'

...

This works fine in irb, but when I stick exactly the same code into an .rb file and try run it with ruby I get <internal:lib/rubygems/custom_require>:29:in require': no such file to load -- excelsior (LoadError)

I guess it has something to do with the load paths apparently being completely different in irb from ruby (I'm on a mac and don't remember exactly how I installed the version of ruby I'm using).

So how do I configure ruby to have the same loadpath as irb?

One extra piece of info: some gems work, but not all :S

Community
  • 1
  • 1
Nat
  • 2,689
  • 2
  • 29
  • 35
  • 1
    Are ruby, gem, and irb all part of the same install? Check with the `which` command. Also, you can modify the `$LOAD_PATH` from either bash or in a ruby script. – Jeremy Jun 14 '11 at 14:24
  • 1
    Very similar question: http://stackoverflow.com/questions/6181247/gem-found-in-irb-not-in-ruby/6181589#6181589 – Andrew Grimm Jun 15 '11 at 00:06

4 Answers4

2

You can easily check what is in your irb load path:

irb(main):001:0> $LOAD_PATH

Then you can identify missing directories and include them in ruby by calling it with -I option (which may be used more than once):

ruby -I missing_dir_1 -I missing_dir_2 your_script.rb

Edit: There is a possibility, though I haven't tested it yet, that by installing Excelsior gem with sudo you've put it in a directory not accessible to ruby ran without sudo. Try sudo ruby your_script.rb.

maro
  • 1,506
  • 13
  • 21
  • is there a way of adding to the load path without having to specify it all on invoking ruby command? say a config file or something? – Nat Jun 14 '11 at 19:59
  • @Nat `$LOAD_PATH << "directory_path"` before `require excelsior` should be enough. – maro Jun 14 '11 at 20:29
  • that doesn't work, which seems odd. I added a `$LOAD_PATH << "directory_path"` statement for each item in the list output by `p $LOAD_PATH` in IRB before the `require 'excelsior'` line but it still chokes with the same error. – Nat Jun 16 '11 at 09:04
  • also running ruby with sudo doesn't seem to make any difference :( – Nat Jun 16 '11 at 09:05
1

What Maro said.

You should also try:

ruby -e 'puts $LOAD_PATH' 

...to see what the differences are to irb.

Edit: Is it possible that you have two different versions of ruby installed? try:

type -a ruby
type -a irb

To see if they link to another executable, like 'irb1.8'.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Andy
  • 1,480
  • 14
  • 17
  • it's possible that i have two versions of ruby, though irb and ruby are both in /usr/bin and /usr/local/bin . the load paths are completely different. Is there a way to change this? – Nat Jun 14 '11 at 20:00
  • It sounds as if you do have two versions of ruby: one in /usr/bin and one in /usr/local/bin. if you ls -l /usr/local/bin/ruby /usr/bin/ruby you might find that these files are actually simlinks to files with longer, more interesting names like ruby1.8, so that should hopefully tell you which one you want to use. You need to make sure that you are running both irb and ruby from the same version. If after all that, your load paths are still different, check out the $RUBYLIB environment variable; that's where load_path should be coming from. – Andy Jun 14 '11 at 21:38
1

To see if the two executables are different versions of ruby (as suspected by some), ask it to do

puts RUBY_VERSION
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
  • ah yes, irb is 1.8.7 and ruby is 1.9.2 ... so i guess i can work out where the ruby tool for 1.8.7 is and use that, though how would i both get irb to use 1.9.2, and get 1.9.2 to work with all me gems??? – Nat Jun 16 '11 at 09:07
  • i found the 1.8.7 ruby tool, and it can 'require excelsior' but it chokes on some other lines which is not unfixable but not convenient either :( any idea how i'd go about making the gems work with the newer ruby? – Nat Jun 16 '11 at 09:19
  • The problem we probably something to do with multiple installs. In the end I fixed it by installing rvm and setting up a new ruby & gems environment. – Nat Jun 28 '11 at 20:55
0

My guess is irb and ruby are running different ruby versions somehow. gem env might help as well.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
rogerdpack
  • 62,887
  • 36
  • 269
  • 388