ruby require problem (something to do with $LOAD_P

2019-07-21 11:05发布

问题:

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

回答1:

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

puts RUBY_VERSION


回答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.



回答3:

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'.



回答4:

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