Comparison __FILE__ and $ 0

0

Recently I saw this decision making in a ruby code:

if __FILE__ == $0

    ....

Then I had to print __FILE__ and $0 separately on irb and I saw that the two pass the same information, my doubts are:

  • Why (when) do this comparison?
  • Why not compare with the same command ( __FILE__ == __FILE__ )?
  • Is there any method to get the filename directly by __FILE__ without having to filter?
asked by anonymous 28.01.2017 / 03:02

1 answer

1

In the ruby-lang.org has a good explanation of what / why. See:

  

__ FILE__ is the magic variable that contains the current file name. $0 is the name of the file used to start the program. Basically it is verified   "If this is the main file being used ..." This allows   that a file be used as a library, and not to run   the code in that context, but if the file is being used as a   executable, run this code.

See more details in this article .

    
28.01.2017 / 04:10