8

I have stumbled across this sintax while reviewing a code in Ruby. The code is:

if __FILE__ == $PROGRAM_NAME
  #some code...
end

I suppose __FILE__ is a variable that gets me the name of the file I am in? But what does $PROGRAM_NAME simbolize then? Also, why is this if statement necessary, since the program works with or without it?

Andrew Schwartz
  • 4,440
  • 3
  • 25
  • 58
Mc-Ac
  • 161
  • 1
  • 8
  • I would say this is a dup of [this SO question](http://stackoverflow.com/questions/224379/what-does-file-mean-in-ruby) posted eight years ago. You could have found that by Googling "ruby \_\_file\_\_" (as I did). It's the first hit. – Cary Swoveland May 08 '16 at 15:10
  • 1
    The point of the question isn't just the meaning of `__FILE__`, but the point of the comparison, so it's a dup of http://stackoverflow.com/questions/4687680/what-does-if-file-0-mean-in-ruby (`$PROGRAM_NAME` and `$0` being the same thing). – Dave Schweisguth May 08 '16 at 15:18
  • @Dave's right. [Here](http://periclestheo.com/2014/02/what-is-up-with-__FILE__-and-$0.html) is a blog post you may find useful. (Hit #5 from my Google search.) – Cary Swoveland May 08 '16 at 15:34

2 Answers2

16

__FILE__ always returns the path of the source file. It's not a variable so you can't assign value to it. Whether it returns a relative path or an absolute one depends on how you run the script.

$PROGRAM_NAME or $0 by default returns the command that boots the program (minus the path of ruby interpreter). For example, you have a script file test.rb like this:

#!/usr/bin/env ruby
puts __FILE__
puts $PROGRAM_NAME

If you run this script with ruby test.rb, it prints

test.rb
test.rb

If you run the script with ruby /path/to/test.rb, it prints

/path/to/test.rb
/path/to/test.rb

If you give the script an execution permission and run it with ./test.rb, it prints

./test.rb
./test.rb

Unlike __FILE__, $PROGRAM_NAME and $0 are real global variables, and you can change their values. $PROGRAM_NAME and $0 are aliases to each other, so you change the value of either one, the value of the other will change accordingly. For example, you have a test2.rb like this:

#!/usr/bin/env ruby
$0 = 'Hello, world!'
puts $0
puts $PROGRAM_NAME

it prints

Hello, world!
Hello, world!
Aetherus
  • 8,720
  • 1
  • 22
  • 36
  • Thank you! This was a great explanation! – Mc-Ac May 08 '16 at 15:27
  • 4
    `$PROGRAM_NAME` is very hard to find in documentation (`$0` in contrast, is easy). In terms of official-ish docs, I could only find `$PROGRAM_NAME` mentioned in the Pragmatic Programmer's book, and only under the `English` module (even though you don't actually need to require English). – Kelvin Jul 12 '16 at 17:58
1

__FILE__ is the current source file name.

It seems to ruby code wants to make sure that the current file corresponds to the program that needs to executed.

$ before a variable, means its a global variable.

Check here to know more - http://www.zenspider.com/Languages/Ruby/QuickRef.html#18

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
coderVishal
  • 8,369
  • 3
  • 35
  • 56