ruby LoadError: 无法加载此类文件[英] ruby LoadError: cannot load such file

本文是小编为大家收集整理的关于ruby LoadError: 无法加载此类文件的处理方法,想解了ruby LoadError: 无法加载此类文件的问题怎么解决?ruby LoadError: 无法加载此类文件问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

当我需要一个文件时,例如(称为 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!\n"
    end
  end

end

在 irb 中我得到:

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

我可能有一个线索,但这只是一个猜测.我的 ruby​​ 版本/安装位置是:

/usr/local/bin/ruby 和 ruby 1.9.3p0

然而,ruby gems 在 /usr/local/lib/ruby/1.9.1 中,它正在谈论版本 1.9.1.会不会是这个原因?

谢谢!

更新奇怪的是,当我在 IRB 中输入"puts RUBY_VERSION"时,我得到了这个:

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!
>> 

推荐答案

st.rb 所在的目录很可能不在你的加载路径上.

假设 st.rb 位于相对于您调用 irb 的位置名为 lib 的目录中,您可以将该 lib 目录添加到 ruby​​ 用来加载类或模块的目录列表中:

$: << 'lib'

例如,为了调用位于 lib 目录中的名为 'foobar' (foobar.rb) 的模块,我需要首先将 lib 目录添加到加载路径列表中.在这里,我只是将 lib 目录附加到我的加载路径:

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

编辑抱歉,我完全错过了您使用 ruby​​ 1.9.x 的事实.所有帐户都报告说您的 出于安全原因,当前工作目录已从 LOAD_PATH 中删除,因此您必须执行类似 irb 中的操作:

$: << "."

本文地址:https://www.itbaoku.cn/post/786447.html