为什么当前目录不在我的 Ruby 路径上?
我当前的工作目录不在我的 Ruby 路径上有什么原因吗?
考虑一下:
~:499$ irb
ruby-1.9.2-p136 :002 > puts $:
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.6.0
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby/1.9.1
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.6.0
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/x86_64-darwin10.6.0
=> nil
这真的让我很困扰,因为 require 没有像我想象的那样工作(尽管我是 ruby nuby):
require 'some_file_that_I_know_darn_well_is_in_pwd.rb'
如果我在末尾附加 '.'
,那么 require 的工作方式如下我预计。
我缺少什么?
更新:
arg!现在我遇到了一个新问题。想一想:
ruby-1.9.2-p136 :010 > `ls`
=> "start.rb\n"
ruby-1.9.2-p136 :011 > require_relative 'start'
LoadError: cannot infer basepath
from (irb):11:in `require_relative'
from (irb):11
from /Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/bin/irb:16:in `<main>'
现在怎么样?
Is there any reason why my present working directory is not on my Ruby path?
Consider:
~:499$ irb
ruby-1.9.2-p136 :002 > puts $:
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.6.0
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby/1.9.1
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.6.0
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/x86_64-darwin10.6.0
=> nil
This is really bothering me because require isn't working as I thought it would (although I'm a ruby nuby):
require 'some_file_that_I_know_darn_well_is_in_pwd.rb'
If I append '.'
to the end, then the require works as I'd expect.
What am I missing?
UPDATE:
Arg! Now I'm getting a new problem. Consider:
ruby-1.9.2-p136 :010 > `ls`
=> "start.rb\n"
ruby-1.9.2-p136 :011 > require_relative 'start'
LoadError: cannot infer basepath
from (irb):11:in `require_relative'
from (irb):11
from /Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/bin/irb:16:in `<main>'
Now what's up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Ruby 1.9.2 中引入的权力 显式更改,使工作目录不再位于 Ruby 路径中。我认为这是世界末日,是一件可怕的事情,直到我了解
require_relative
。我的应用程序往往看起来像这样:然后 lib/init.rb 可以具有:
这是蜜蜂的膝盖,解决了我过去需要来自不同工作目录的相同文件的各种问题。
编辑:不幸的是(由于我不知道也没有研究过的原因)
require_relative
并不专门在 irb 中工作。为此,您可以:$: << '.'
或$:.unshift '.'
,或您可以使用
load 'myfile.rb'
或require '. /myfile'
改为:In Ruby 1.9.2 the Powers that Be introduced an explicit change so that the working directory is no longer in the Ruby path. I thought it was the Apocalypse and a terrible thing, until I learned about
require_relative
. My apps tend to look like this:And then
lib/init.rb
can have:It's the bees knees, and solves all sorts of problems I used to have with requiring the same file from different working directories.
Edit: Unfortunately (for reasons I don't know and haven't looked into)
require_relative
doesn't work specifically in irb. For this you can:$: << '.'
or$:.unshift '.'
, oryou can use
load 'myfile.rb'
orrequire './myfile'
instead:您可以使用 require_relative 假设它满足您的需要。
You can use require_relative assuming it does what you need.
确保使用可以在其中找到自定义 *.rb 代码的所有目录路径设置环境变量“RUBYLIB”。这也让我抓狂。
Make sure that the environment variable "RUBYLIB" is set with all directory paths where you will find custom *.rb code. It drove me nuts too.