Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
foo.rb 是一个单行puts __FILE__
puts __FILE__
irb -r ./foo.rb
给了我到 foo.rb 的绝对路径。如果我运行,这不是行为ruby foo.rb。为什么它发生在irb?
ruby foo.rb
irb 0.9.6(09/06/30),红宝石 1.9.3p327
在 IRB 中发生这种情况的原因与如果您编写了一个以bar.rb以下内容命名的程序会发生这种情况的原因相同。
bar.rb
require './foo'
你会发现像你一样使用带有 require 的 IRB 与调用带有 require 的 IRB 没有什么不同bar.rb。
从文档中:
__FILE__-- 当前正在执行的文件的名称,包括相对于启动应用程序的目录(或当前目录,如果已更改)的路径。
__FILE__
所以这包括相对于应用程序启动目录的路径。谁知道irb或bar在哪里?当不清楚时,也会给出路径。