Why does irb -r make __FILE__ an absolute path?

寵の児 提交于 2019-12-11 10:03:07

问题


foo.rb is a one-liner that puts __FILE__

irb -r ./foo.rb

gives me the absolute path to foo.rb. This is not the behavior if I run ruby foo.rb. Why is it happening in irb?

irb 0.9.6(09/06/30), ruby 1.9.3p327


回答1:


The reason this happens in IRB is the same reason this would happen if you had written a program named bar.rb with the following contents.

require './foo'

You will find that using IRB with the require as you do is no different than calling bar.rb which has the require.

From the documentation:

__FILE__ -- The name of the file currently being executed, including path relative to the directory where the application was started up (or the current directory, if it has been changed).

So this is including the path relative to the directory where the application was started up. Who knows where irb or bar are? When it is not clear, then the path is given as well.



来源:https://stackoverflow.com/questions/13874115/why-does-irb-r-make-file-an-absolute-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!