如何在ruby文件中获取程序的“入口点”?

时间:2012-10-23 10:43:24

标签: ruby module path

我有三个ruby文件,a.rbb.rbc.rb如下:

要执行c.rb我可以ruby a.rbruby lib/lib/c.rb。但我想区分这两种方法。所以在c.rb我想知道程序的切入点。 (如果您熟悉节点,则它与require.main相同。)

dir
 -- a.rb
 -- lib
    -- b.rb
    -- lib
       -- c.rb

# a.rb
require 'lib/b'

# b.rb
require 'lib/c'

# c.rb
puts 'I am here'

1 个答案:

答案 0 :(得分:4)

这样做的经典方法是使用__FILE__$0

if $0 == __FILE__
  # code when this file is the entry point
end