Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

rubygems - ruby LoadError: cannot load such file

When I require a file, for example (called st.rb):

require 'rubygems'
require 'mongrel'

class TestHandler < Mongrel::HttpHandler

  def process(request, response)
    response.start(200) do |head, out|
      head["Content-Type"] = "text/html"
      out.write "Hello, World!
"
    end
  end

end

in irb I get:

>> require 'st.rb'
LoadError: cannot load such file -- st.rb
    from /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from (irb):3
    from /usr/local/bin/irb:12:in `<main>'

I might have a clue, but it's just a guess. My ruby version/install location is:

/usr/local/bin/ruby and ruby 1.9.3p0

yet, ruby gems is in /usr/local/lib/ruby/1.9.1 and it's talking about version 1.9.1. Could this possibly be the reason?

Thanks!

UPDATE Weird, when I type 'puts RUBY_VERSION' in IRB, I get this:

puts RUBY_VERSION
1.9.3
NoMethodError: undefined method `write' for nil:NilClass
    from /usr/local/lib/ruby/1.9.1/irb.rb:311:in `printf'
    from /usr/local/lib/ruby/1.9.1/irb.rb:311:in `output_value'
    from /usr/local/lib/ruby/1.9.1/irb.rb:160:in `block (2 levels) in eval_input'
    from /usr/local/lib/ruby/1.9.1/irb.rb:273:in `signal_status'
    from /usr/local/lib/ruby/1.9.1/irb.rb:156:in `block in eval_input'
    from /usr/local/lib/ruby/1.9.1/irb/ruby-lex.rb:243:in `block (2 levels) in each_top_level_statement'
    from /usr/local/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `loop'
    from /usr/local/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `block in each_top_level_statement'
    from /usr/local/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `catch'
    from /usr/local/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `each_top_level_statement'
    from /usr/local/lib/ruby/1.9.1/irb.rb:155:in `eval_input'
    from /usr/local/lib/ruby/1.9.1/irb.rb:70:in `block in start'
    from /usr/local/lib/ruby/1.9.1/irb.rb:69:in `catch'
    from /usr/local/lib/ruby/1.9.1/irb.rb:69:in `start'
    from /usr/local/bin/irb:12:in `<main>'
Maybe IRB bug!
>> 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The directory where st.rb lives is most likely not on your load path.

Assuming that st.rb is located in a directory called lib relative to where you invoke irb, you can add that lib directory to the list of directories that ruby uses to load classes or modules with this:

$: << 'lib'

For example, in order to call the module called 'foobar' (foobar.rb) that lives in the lib directory, I would need to first add the lib directory to the list of load path. Here, I am just appending the lib directory to my load path:

irb(main):001:0> require 'foobar'
LoadError: no such file to load -- foobar
        from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
        from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
        from (irb):1
irb(main):002:0> $:
=> ["/usr/lib/ruby/gems/1.8/gems/spoon-0.0.1/lib", "/usr/lib/ruby/gems/1.8/gems/interactive_editor-0.0.10/lib", "/usr/lib/ruby/site_ruby/1.8", "/usr/lib/ruby/site_ruby/1.8/i386-cygwin", "/usr/lib/ruby/site_ruby", "/usr/lib/ruby/vendor_ruby/1.8", "/usr/lib/ruby/vendor_ruby/1.8/i386-cygwin", "/usr/lib/ruby/vendor_ruby", "/usr/lib/ruby/1.8", "/usr/lib/ruby/1.8/i386-cygwin", "."]
irb(main):004:0> $: << 'lib'
=> ["/usr/lib/ruby/gems/1.8/gems/spoon-0.0.1/lib", "/usr/lib/ruby/gems/1.8/gems/interactive_editor-0.0.10/lib", "/usr/lib/ruby/site_ruby/1.8", "/usr/lib/ruby/site_ruby/1.8/i386-cygwin", "/usr/lib/ruby/site_ruby", "/usr/lib/ruby/vendor_ruby/1.8", "/usr/lib/ruby/vendor_ruby/1.8/i386-cygwin", "/usr/lib/ruby/vendor_ruby", "/usr/lib/ruby/1.8", "/usr/lib/ruby/1.8/i386-cygwin", ".", "lib"]
irb(main):005:0> require 'foobar'
=> true

EDIT Sorry, I completely missed the fact that you are using ruby 1.9.x. All accounts report that your current working directory has been removed from LOAD_PATH for security reasons, so you will have to do something like in irb:

$: << "."

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...