ruby require problem (something to do with $LOAD_PATH)

元气小坏坏 提交于 2019-12-11 03:20:09

问题


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.



来源:https://stackoverflow.com/questions/6344451/ruby-require-problem-something-to-do-with-load-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!