ruby LoadError:无法加载这样的文件 [英] ruby LoadError: cannot load such file
问题描述
当我需要一个文件时,例如(叫做st.rb):
pre $ require'rubygems'
需要'mongrel'
class TestHandler< Mongrel :: HttpHandler
def process(request,response)
response.start(200)do | head,out |
head [Content-Type] =text / html
out.writeHello,World!\\\
end
end
结束
在irb中我得到:
>>需要'st.rb'
LoadError:无法加载这样的文件 - /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in`require中的st.rb
'从/usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in'require'
from(irb):3 $ b $ from / usr / local / bin / irb:12:在< main>'
我可能有线索,但这只是一个猜测。我的ruby版本/安装位置是:
$ b / usr / local / bin / ruby 和 ruby 1.9.3p0
但是,ruby gems位于 /usr/local/lib/ruby/1.9中。 1
,它正在讨论版本 1.9.1
。这可能是原因吗?
谢谢!
更新
奇怪的是,当我在IRB中键入'RUBY_VERSION'时,我得到这个:
puts RUBY_VERSION
1.9.3
NoMethodError:未定义的方法'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水平)来自/usr/local/lib/ruby/1.9.1的/usr/local/lib/ruby/1.9.1/irb.rb:273:in`signal_status'
的eval_input'
/irb.rb:156:in`在eval_input中阻止'
从/usr/local/lib/ruby/1.9.1/irb/ruby-lex.rb:243:in`block(2级)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`在each_top_level_statement中阻止'$ / $ / b / from /usr/local/lib/ruby/1.9.1/irb/ruby- lex.rb:228:在`catch'中
从/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:在`eval_input'
/usr/local/lib/ruby/1.9.1/irb.rb:70:in`block in start' $ / b / b from /usr/local/lib/ruby/1.9.1/irb.rb:69:in`catch'
from /usr/local/lib/ruby/1.9.1/irb.rb: 69:在`开始'
从/ usr / local / bin / irb:12:在< main>'
可能IRB错误!
>>
st所在的目录。 rb
lives很可能不在您的加载路径中。
假设 st.rb
位于相对于您调用 irb
的目录中,名为 lib
的目录中,您可以添加 lib
目录添加到ruby用来加载类或模块的目录列表中:
$:<< 'lib'
例如,为了调用名为'foobar'的模块(foobar.rb)它位于 lib
目录中,我需要先将 lib
目录添加到加载路径列表中。在这里,我只是将 lib
目录附加到我的加载路径中:
IRB(主):001:0>需要 'foobar的'
LoadError:没有这样的文件来加载 - 从/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in`gem_original_require
foobar的
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中/红宝石/ site_ruby, /usr/lib/ruby/vendor_ruby/1.8 /usr/lib/ruby/vendor_ruby/1.8/i386-cygwin, / usr / lib中/红宝石/ 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中/红宝石/ 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>需要'foobar'
=> true
编辑
对不起,我完全错过了你使用的是Ruby 1.9.x.所有帐户均报告您的出于安全原因,当前工作目录已从 LOAD_PATH
中删除,因此您必须执行类似于 irb
$:<<
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!\n"
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!
>>
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
:
$: << "."
这篇关于ruby LoadError:无法加载这样的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!