Forum: Ruby Explanation of if __FILE__ == $0

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
290cf664d9e6f823fc3af57556493db7?d=identicon&s=25 PJ Hyett (Guest)
on 2006-03-17 17:49
(Received via mailing list)
So I've tracked this down:
 __FILE__ is the name of the current source file
$0 at the top level is the name of the top-level program being executed

But I'm still confused as to what this if statement is accomplishing?
It's obviously useful for something since it's used quite often. Does
this pertain to when a script is called from another script?

Thanks,
PJ
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-17 17:55
(Received via mailing list)
Hi --

On Sat, 18 Mar 2006, PJ Hyett wrote:

> So I've tracked this down:
> __FILE__ is the name of the current source file
> $0 at the top level is the name of the top-level program being executed
>
> But I'm still confused as to what this if statement is accomplishing?
> It's obviously useful for something since it's used quite often. Does
> this pertain to when a script is called from another script?

Yes; if __FILE__ (the name of the current file) is the same as $0 (the
file where execution started), then you're in the startup file.
You'll often see this used in, say, a library file that usually gets
loaded from another file (so __FILE__ == $0 is false), but can also be
run standalone for the sake of running tests.


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-03-17 17:58
(Received via mailing list)
On Mar 17, 2006, at 5:49 PM, PJ Hyett wrote:

> PJ
It is used when you want to run something when the file is executed
directly. Some put tests in their code like this, I think this is bad
form. Tests belong in a separate file.

File a.rb
---------
class Foo
end

if __FILE__ == $PROGRAM_NAME # [1]
   puts "Testing out class Foo"
   # ...
end

File b.rb
---------
require 'a'
puts "The if block in a.rb is not executed"

__END__

$ruby a.rb
Testing out class Foo
$ruby b.rb
The if block in a.rb is not executed

[1] $PROGRAM_NAME is the less cryptic name for $0

-- Daniel
A6ce942e03edad55d9b504c1e1d859d6?d=identicon&s=25 Jim Freeze (Guest)
on 2006-03-17 18:08
(Received via mailing list)
On Mar 17, 2006, at 10:58 AM, Daniel Harple wrote:

> if __FILE__ == $PROGRAM_NAME # [1]
> [1] $PROGRAM_NAME is the less cryptic name for $0
>

Most definitely. I would like to see this changed in existing code.

Being a long time Ruby user, I always used $0 until
recently, Then I read some code by a new ruby user (whom I introduced
to ruby).
I never bothered to see if an alternative to $0 existed.

You can learn from anyone. Even the nubies. :)

Jim Freeze
290cf664d9e6f823fc3af57556493db7?d=identicon&s=25 PJ Hyett (Guest)
on 2006-03-17 19:09
(Received via mailing list)
Thanks guys, that's helpful.

-PJ
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2014-02-06 12:04
I agree with Jim Freeze - I think

  if __FILE__ == $PROGRAM_NAME

is slightly more readable than

  if __FILE__ == $0

Elegance is what keeps me attached to ruby.
This topic is locked and can not be replied to.