2012-02-06 93 views
19

當我需要一個文件,例如(稱爲st.rb):紅寶石LoadError:無法加載此類文件

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

我可能有一個線索,但這只是一個猜測。我的紅寶石版本/安裝位置是:

/usr/local/bin/rubyruby 1.9.3p0

然而,紅寶石的寶石是/usr/local/lib/ruby/1.9.1,它在談論1.9.1版本。這可能是原因嗎?

謝謝!

UPDATE 奇怪,當我輸入IRB '使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! 
>> 
+0

如果你只是'需要'st''怎麼辦? – 2012-02-06 01:00:17

+0

@MatthewLehner同樣的東西 – 0xSina 2012-02-06 01:18:16

+0

[爲什麼不是我的Ruby路徑上的當前目錄?](http://stackoverflow.com/questions/4965556/why-isnt-current-directory-on-my-ruby-path );請參閱我在該問題中的解答以瞭解解決方法。 – Phrogz 2012-02-06 05:17:05

回答

16

所在的目錄st.rb生命是最有可能不是你的負載路徑。

假設st.rb位於一個名爲lib相對於在那裏你調用irb目錄,你可以添加lib目錄目錄的列表紅寶石用來加載類或模塊與此:

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

編輯 對不起,我完全錯過了您正在使用紅寶石1.9.x中的事實所有帳戶報告您current working directory has been removed from LOAD_PATH for security reasons,所以你將不得不做這樣的事情在irb

$: << "." 
+0

我目前被cd到了st.rb所在的目錄中。這工作正常,直到昨天,當我決定升級紅寶石1.9.3 – 0xSina 2012-02-06 02:54:11

8

如果您指定路徑,則問題應該已解決。

例如

「需要 'st.rb'」 - > 「要求 './st.rb'」

看看您的問題得到解決或沒有。

+0

Thx。在處理AWS樣本時,我遇到了這個問題。事實上,如果我們試圖在示例代碼中匹配ruby的版本,問題可能會消失。 – 2014-10-30 10:34:34

1

我剛碰到類似的問題。嘗試

require './st.rb' 

這應該是訣竅。

相關問題