__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!
__FILE__
, but the point of the comparison, so it's a dup of #4688180 ($PROGRAM_NAME
and$0
being the same thing). – Brotherly