Rails irb 默认目录

发布于 2024-09-24 10:40:32 字数 1294 浏览 2 评论 0原文

我试图在运行 irb 时包含源代码文件,但 irb 无法找到它。

例如,假设我位于终端中的以下目录中:

/dan/rubyapp/

假设我在 /dan/rubyapp/ 中有一个名为“firstapp.rb”的文件

,我启动 irb,并在 irb 提示符下键入,

> require "firstapp.rb"

但找不到该文件。如果我输入“Dir.pwd”,它会显示为“

/dan/rubyapp/

我可以让“require”工作的唯一方法是,如果我包含完整路径,就像这样,

> require "/dan/rubyapp/firstapp.rb"

这是我可以让它工作的唯一方法吗?我在网上看到的所有教程都只是“需要文件名”,所以我认为它会起作用。


这是 irb 的 $: 的输出

ruby-1.9.2-p0 > $:
 => ["/Users/Daniel/.rvm/gems/ruby-1.9.2-p0/gems/wirble-0.1.3/bin", 
"/Users/Daniel/.rvm/gems/ruby-1.9.2-p0/gems/wirble-0.1.3/lib", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.4.0", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby/1.9.1", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.4.0", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1",     
"/Users/Daniel/.rvm/rubies/ruby-
1.9.2-p0/lib/ruby/1.9.1/x86_64-darwin10.4.0"] 

I'm trying to include a source code file when I run irb but irb is unable to find it.

For example, say I am in the following directory in terminal:

/dan/rubyapp/

Assume I have a file named "firstapp.rb" in /dan/rubyapp/

I startup irb and from the irb prompt I type

> require "firstapp.rb"

but the file can't be found. If I type "Dir.pwd" it shows as

/dan/rubyapp/

The only way I can get "require" to work is if I include the full path like so

> require "/dan/rubyapp/firstapp.rb"

Is that the only way I can get this to work? All the tutorials I see online simply do "require file_name" so I assumed it would work.


here is the output from $: at irb

ruby-1.9.2-p0 > $:
 => ["/Users/Daniel/.rvm/gems/ruby-1.9.2-p0/gems/wirble-0.1.3/bin", 
"/Users/Daniel/.rvm/gems/ruby-1.9.2-p0/gems/wirble-0.1.3/lib", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.4.0", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby/1.9.1", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.4.0", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1",     
"/Users/Daniel/.rvm/rubies/ruby-
1.9.2-p0/lib/ruby/1.9.1/x86_64-darwin10.4.0"] 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

满地尘埃落定 2024-10-01 10:40:32

问题是当前工作目录不再位于您的路径中(从 Ruby 1.9.2 开始)。有几种不同的方法可以解决这个问题。

1) 在 ruby​​ 文件本身中,您可以使用 require_relative 方法来代替 require。这将加载一个相对于包含 require_relative 方法的文件位置的文件:
http://extensions.rubyforge.org/rdoc/classes/Kernel.html

require_relative 'firstapp.rb'

然而,这在 irb 中不起作用。

2) 您的另一个选择是将当前路径包含在 require 方法的参数中。这将在 irb 或 ruby​​ 文件中工作。例如:

require './firstapp.rb'

在 ruby​​ 中实现这一点的原因是为了避免在路径中的不同目录中存在同名的不同文件时无意中要求错误的文件(类似于 *nix 在其路径中不包含当前目录“.”)小路)

The problem is that the current working directory is no longer in your path (as of Ruby 1.9.2). There are a few different ways around the problem.

1) In a ruby file itself, you can use the method require_relative instead of require. This will load a file relative to the loaction of the file containing the require_relative method:
http://extensions.rubyforge.org/rdoc/classes/Kernel.html

require_relative 'firstapp.rb'

This, however, will not work in irb.

2) Your other option is to include the current path in your argument to the require method. This will work in irb or in a ruby file. For instance:

require './firstapp.rb'

The reason this was implemented in ruby was to avoid inadvertently requiring the wrong file if there are different files with the same name in different directories in the path (similar to how *nix does not include the current directory "." in its path)

十年不长 2024-10-01 10:40:32

可以尝试以下几件事:

1)从 require 末尾删除 .rb,这样你就可以:

require 'firstapp'

通常不会将 .rb 添加到 require(仅添加到加载) - 请查看此处以获取更多详细信息:
http://www.fromjavatoruby.com/2008/10/require- vs-load.html

2) 如果失败,请确保当前目录位于您的加载路径上 - 在 irb 中执行:

 p $:

它将打印出您的 ruby​​ 加载路径 - 检查“.”条目(我的是最后一个条目)

A couple of things to try:

1) Drop the .rb from the end of your require so you have:

require 'firstapp'

You don't normally add the .rb to a require (only to a load) - have a look here for more details:
http://www.fromjavatoruby.com/2008/10/require-vs-load.html

2) Failing that, make sure the current directory is on your load path - in irb execute:

 p $:

and it will print out your ruby load path - check for an entry for "." (mine is the last entry)

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