当我需要一个文件时,例如(名为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我得到:
>> 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中键入'put 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 :(得分:18)
st.rb
所居住的目录很可能不在您的加载路径上。
假设st.rb
位于名为lib
的目录中,相对于您调用irb
的位置,可以将该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的事实。所有帐户都会报告您的current working directory has been removed from LOAD_PATH
for security reasons,因此您必须执行irb
中的操作:
$: << "."
答案 1 :(得分:11)
为安全起见其他原因,ruby默认情况下不包含load_path中的当前目录。您可能需要查看此内容以获取更多详细信息 - Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative?
答案 2 :(得分:9)
如果指定路径,问题就解决了。
e.g。
“require'st.rb'” - &gt; “require'./st.rb'”
查看您的问题是否得到解决。
答案 3 :(得分:1)
我刚遇到类似的问题。尝试
require './st.rb'
这应该可以解决问题。
答案 4 :(得分:0)
我创建了自己的Gem,但是我在加载路径之外的目录中创建了它:
$ pwd
/Users/myuser/projects
$ gem build my_gem/my_gem.gemspec
然后我运行irb
并尝试加载宝石:
> require 'my_gem'
LoadError: cannot load such file -- my_gem
我使用全局变量$:检查我的加载路径,并且意识到我正在使用RVM。 rvm在我的加载路径$:
中有特定的目录。这些目录都不包含我创建自定义gem的〜/ projects目录。
因此,一种解决方案是修改加载路径本身:
$: << "/Users/myuser/projects/my_gem/lib"
请注意,lib目录位于路径中,该路径包含my_gem.rb文件,该文件在irb中是必需的:
> require 'my_gem'
=> true
现在,如果要在RVM路径中安装gem,则需要运行:
$ gem install my_gem
但是它必须位于rubygems.org之类的存储库中。
$ gem push my_gem-0.0.0.gem
Pushing gem to RubyGems.org...
Successfully registered gem my_gem