Rails irb 默认目录
我试图在运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是当前工作目录不再位于您的路径中(从 Ruby 1.9.2 开始)。有几种不同的方法可以解决这个问题。
1) 在 ruby 文件本身中,您可以使用 require_relative 方法来代替 require。这将加载一个相对于包含 require_relative 方法的文件位置的文件:
http://extensions.rubyforge.org/rdoc/classes/Kernel.html
然而,这在 irb 中不起作用。
2) 您的另一个选择是将当前路径包含在 require 方法的参数中。这将在 irb 或 ruby 文件中工作。例如:
在 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
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:
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)
可以尝试以下几件事:
1)从 require 末尾删除 .rb,这样你就可以:
通常不会将 .rb 添加到 require(仅添加到加载) - 请查看此处以获取更多详细信息:
http://www.fromjavatoruby.com/2008/10/require- vs-load.html
2) 如果失败,请确保当前目录位于您的加载路径上 - 在 irb 中执行:
它将打印出您的 ruby 加载路径 - 检查“.”条目(我的是最后一个条目)
A couple of things to try:
1) Drop the .rb from the end of your require so you have:
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:
and it will print out your ruby load path - check for an entry for "." (mine is the last entry)