ruby LoadError:无法加载此类文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9154365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ruby LoadError: cannot load such file
提问by 0xSina
When I require a file, for example (called st.rb):
例如,当我需要一个文件时(称为 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:
在 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>'
I might have a clue, but it's just a guess. My ruby version/install location is:
我可能有线索,但这只是一个猜测。我的 ruby 版本/安装位置是:
/usr/local/bin/rubyand ruby 1.9.3p0
/usr/local/bin/ruby和 ruby 1.9.3p0
yet, ruby gems is in /usr/local/lib/ruby/1.9.1and it's talking about version 1.9.1. Could this possibly be the reason?
然而,ruby gems 已经出现/usr/local/lib/ruby/1.9.1并且它正在谈论 version 1.9.1。这可能是原因吗?
Thanks!
谢谢!
UPDATEWeird, when I type 'puts RUBY_VERSION' in IRB, I get this:
更新奇怪,当我在 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!
>>
回答by buruzaemon
The directory where st.rblives is most likely not on your load path.
所在的目录st.rb很可能不在您的加载路径上。
Assuming that st.rbis located in a directory called librelative to where you invoke irb, you can add that libdirectory to the list of directories that ruby uses to load classes or modules with this:
假设它st.rb位于lib相对于您调用的位置的目录中irb,您可以将该lib目录添加到 ruby 用于加载类或模块的目录列表中:
$: << 'lib'
For example, in order to call the module called 'foobar' (foobar.rb) that lives in the libdirectory, I would need to first add the libdirectory to the list of load path. Here, I am just appending the libdirectory to my load path:
例如,为了调用位于目录中的名为 'foobar' (foobar.rb) 的模块lib,我需要先将该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
EDITSorry, 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_PATHfor security reasons, so you will have to do something like in irb:
编辑对不起,我完全错过了你使用 ruby 1.9.x 的事实。所有帐户都报告出于安全原因已从LOAD_PATH您当前的工作目录中删除,因此您必须执行以下操作irb:
$: << "."
回答by Dhruva Sagar
For security & other reasons, ruby does not by default include the current directory in the load_path. You may want to check this for more details - Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative?
出于安全和其他原因,ruby 默认情况下不会在 load_path 中包含当前目录。您可能需要查看更多详细信息 -为什么 Ruby 1.9.2 删除“。” 来自 LOAD_PATH,还有什么选择?
回答by Tom K. C. Chiu
The problem shall have solved if you specify your path.
如果你指定你的路径,问题应该已经解决了。
e.g.
例如
"require 'st.rb'" --> "require './st.rb'"
“需要'st.rb'”-->“需要'./st.rb'”
See if your problem get solved or not.
看看你的问题是否得到解决。
回答by Faisal Hassan
I just came across a similar problem. Try
我刚刚遇到了类似的问题。尝试
require './st.rb'
This should do the trick.
这应该可以解决问题。
回答by Donato
I created my own Gem, but I did it in a directory that is not in my load path:
我创建了自己的 Gem,但我是在一个不在我的加载路径中的目录中创建的:
$ pwd
/Users/myuser/projects
$ gem build my_gem/my_gem.gemspec
Then I ran irband tried to load the Gem:
然后我跑去irb尝试加载 Gem:
> require 'my_gem'
LoadError: cannot load such file -- my_gem
I used the global variable $: to inspect my load path and I realized I am using RVM. And rvm has specific directories in my load path $:. None of those directories included my ~/projects directory where I created the custom gem.
我使用全局变量 $: 来检查我的加载路径,我意识到我正在使用 RVM。并且 rvm 在我的加载路径中有特定的目录$:。这些目录都不包含我创建自定义 gem 的 ~/projects 目录。
So one solution is to modify the load path itself:
所以一种解决方案是修改加载路径本身:
$: << "/Users/myuser/projects/my_gem/lib"
Note that the lib directory is in the path, which holds the my_gem.rb file which will be required in irb:
请注意,lib 目录位于路径中,该路径包含 irb 所需的 my_gem.rb 文件:
> require 'my_gem'
=> true
Now if you want to install the gem in RVM path, then you would need to run:
现在,如果要在 RVM 路径中安装 gem,则需要运行:
$ gem install my_gem
But it will need to be in a repository like rubygems.org.
但它需要在像 rubygems.org 这样的存储库中。
$ gem push my_gem-0.0.0.gem
Pushing gem to RubyGems.org...
Successfully registered gem my_gem

