Run a Ruby library from the command-line

前端 未结 3 508
小蘑菇
小蘑菇 2020-12-29 08:00

I\'ve just learned the basics of Ruby after being very happy with Python for several years (I\'m still using Python for some things), but I\'d like to know if there\'s an id

相关标签:
3条回答
  • 2020-12-29 08:53

    While

    if __FILE__ == $0
      Foo.run
    end
    

    is the common approach, I'm currently using

    if File.identical?(__FILE__, $0)
      Foo.run
    end
    

    because programs like ruby-prof can make $0 not equal __FILE__ even when you use --replace-progname.

    0 讨论(0)
  • 2020-12-29 08:54

    You can find a similar functionality in ruby.

    __FILE__ the current source file name.

    $0 Contains the name of the script being executed. May be assignable.

    source: Ruby Quick Ref

    0 讨论(0)
  • 2020-12-29 08:56

    You want to use:

    if __FILE__ == $0
      # do stuff
    end
    

    __FILE__ is the source file name and $0 is the name of the script currently being executed.

    0 讨论(0)
提交回复
热议问题