Ruby`require`调用在自定义代码上失败
我发现使用require
来加载像Sinatra Web框架这样的东西我没有问题,但我似乎无法使用require
来加载我自己的自定义代码?
例如,我有两个文件:
- test1.rb
- test2.rb
‘test1.rb’的内容是:
#!/usr/bin/env ruby require 'test2'
‘test2.rb’的内容是:
class String def vowels self.scan(/[aeiou]/i) end end
如果我尝试运行ruby test1.rb
然后我得到以下错误…
/Users//.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- test2 (LoadError) from /Users//.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require' from test1.rb:3:in `'
然后我注意到如果我尝试使用shotgun
gem加载Web服务器,那么我得到一个不同但更详细的错误堆栈跟踪…
Boot Error Something went wrong while loading test1.rb LoadError: cannot load such file -- test2 /Users//.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require' /Users//.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require' /Users//Dropbox/Library/Ruby/Passage/test1.rb:3:in `' /Users//.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require' /Users//.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require' /Users//.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/loader.rb:114:in `inner_app' /Users//.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/loader.rb:102:in `assemble_app' /Users//.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/loader.rb:86:in `proceed_as_child' /Users//.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/loader.rb:31:in `call!' /Users//.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/loader.rb:18:in `call' /Users//.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/favicon.rb:12:in `call' /Users//.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/static.rb:14:in `call' /Users//.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/builder.rb:134:in `call' /Users//.rvm/gems/ruby-1.9.3-p125/gems/thin-1.3.1/lib/thin/connection.rb:80:in `block in pre_process' /Users//.rvm/gems/ruby-1.9.3-p125/gems/thin-1.3.1/lib/thin/connection.rb:78:in `catch' /Users//.rvm/gems/ruby-1.9.3-p125/gems/thin-1.3.1/lib/thin/connection.rb:78:in `pre_process' /Users//.rvm/gems/ruby-1.9.3-p125/gems/thin-1.3.1/lib/thin/connection.rb:53:in `process' /Users//.rvm/gems/ruby-1.9.3-p125/gems/thin-1.3.1/lib/thin/connection.rb:38:in `receive_data' /Users//.rvm/gems/ruby-1.9.3-p125/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine' /Users//.rvm/gems/ruby-1.9.3-p125/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run' /Users//.rvm/gems/ruby-1.9.3-p125/gems/thin-1.3.1/lib/thin/backends/base.rb:61:in `start' /Users//.rvm/gems/ruby-1.9.3-p125/gems/thin-1.3.1/lib/thin/server.rb:159:in `start' /Users//.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/handler/thin.rb:13:in `run' /Users//.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/bin/shotgun:156:in `' /Users//.rvm/gems/ruby-1.9.3-p125/bin/shotgun:19:in `load' /Users//.rvm/gems/ruby-1.9.3-p125/bin/shotgun:19:in `' /Users//.rvm/gems/ruby-1.9.3-p125/bin/ruby_noexec_wrapper:14:in `eval' /Users//.rvm/gems/ruby-1.9.3-p125/bin/ruby_noexec_wrapper:14:in `'
我不确定问题是什么?
任何帮助表示赞赏。
谢谢。
当前目录(相对于主ruby文件)不是加载路径( require
查找的路径集)的一部分。 所以你需要将它添加到加载路径(最好通过调用ruby作为ruby -I. test1.rb
),使用require "./test2.rb"
或使用require_relative "test2.rb"
,这需要文件相对于文件的目录。