为什么当前目录不在我的 Ruby 路径上?

发布于 2024-10-17 02:04:18 字数 1332 浏览 3 评论 0原文

我当前的工作目录不在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剪不断理还乱 2024-10-24 02:04:18

Ruby 1.9.2 中引入的权力 显式更改,使工作目录不再位于 Ruby 路径中。我认为这是世界末日,是一件可怕的事情,直到我了解 require_relative。我的应用程序往往看起来像这样:

require 'some_gem'
require 'another_gem'
require_relative 'lib/init'

然后 lib/init.rb 可以具有:

require_relative 'lib1' # this is lib/lib1.rb
require_relative 'lib2' # this is lib/lib2.rb

这是蜜蜂的膝盖,解决了我过去需要来自不同工作目录的相同文件的各种问题。

编辑:不幸的是(由于我不知道也没有研究过的原因)require_relative 并不专门在 irb 中工作。为此,您可以:

  1. 执行您最初描述的操作: $: << '.'$:.unshift '.',或
  2. 您可以使用 load 'myfile.rb'require '. /myfile' 改为:

    irb(主):001:0>目录['*.rb']
    => [“a.rb”,“bar.rb”,“foo.rb”,“prime.rb”,“tmp.rb”]
    
    irb(主):002:0>需要“a”
    LoadError:没有要加载的文件--a
      来自 :29:in `require'
      来自 :29:in `require'
      来自(irb):2
      来自 /usr/local/bin/irb:12:in `
    ' irb(主):003:0> require_relative 'a' LoadError:无法推断基本路径 来自(irb):3:在“require_relative”中 来自(irb):3 来自 /usr/local/bin/irb:12:in `
    ' irb(主):004:0>加载“a.rb” 一个 =>真的 irb(主):005:0>需要'./a' 一个 =>真的

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:

require 'some_gem'
require 'another_gem'
require_relative 'lib/init'

And then lib/init.rb can have:

require_relative 'lib1' # this is lib/lib1.rb
require_relative 'lib2' # this is lib/lib2.rb

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:

  1. do what you initially described: either $: << '.' or $:.unshift '.', or
  2. you can use load 'myfile.rb' or require './myfile' instead:

    irb(main):001:0> Dir['*.rb']
    => ["a.rb", "bar.rb", "foo.rb", "prime.rb", "tmp.rb"]
    
    irb(main):002:0> require 'a'
    LoadError: no such file to load -- a
      from <internal:lib/rubygems/custom_require>:29:in `require'
      from <internal:lib/rubygems/custom_require>:29:in `require'
      from (irb):2
      from /usr/local/bin/irb:12:in `<main>'
    
    irb(main):003:0> require_relative 'a'
    LoadError: cannot infer basepath
      from (irb):3:in `require_relative'
      from (irb):3
      from /usr/local/bin/irb:12:in `<main>'
    
    irb(main):004:0> load 'a.rb'
    a
    => true
    
    irb(main):005:0> require './a'
    a
    => true
    
长不大的小祸害 2024-10-24 02:04:18

您可以使用 require_relative 假设它满足您的需要。

You can use require_relative assuming it does what you need.

孤千羽 2024-10-24 02:04:18

确保使用可以在其中找到自定义 *.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文