# Explanation of if \_\_FILE\_\_ == $0

**URL:** <https://www.ruby-forum.com/t/explanation-of-if-file-0/55180>\
**Category:** Ruby\
**Created:** [March 17, 2006, 5:49pm UTC](https://www.ruby-forum.com/t/explanation-of-if-file-0/55180 "2006-03-17T17:49:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PJ\_Hyett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@PJ\_Hyett](https://www.ruby-forum.com/u/PJ_Hyett)\
**Post date:** [March 17, 2006, 5:49pm UTC](https://www.ruby-forum.com/t/explanation-of-if-file-0/55180/1 "2006-03-17T17:49:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PJ\_Hyett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@PJ\_Hyett](https://www.ruby-forum.com/u/PJ_Hyett)\
**Post date:** [March 17, 2006, 5:55pm UTC](https://www.ruby-forum.com/t/explanation-of-if-file-0/55180/2 "2006-03-17T17:55:53Z")

</div>

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 (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![PJ\_Hyett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@PJ\_Hyett](https://www.ruby-forum.com/u/PJ_Hyett)\
**Post date:** [March 17, 2006, 5:58pm UTC](https://www.ruby-forum.com/t/explanation-of-if-file-0/55180/3 "2006-03-17T17:58:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PJ\_Hyett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@PJ\_Hyett](https://www.ruby-forum.com/u/PJ_Hyett)\
**Post date:** [March 17, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/explanation-of-if-file-0/55180/4 "2006-03-17T18:08:19Z")

</div>

On Mar 17, 2006, at 10:58 AM, Daniel H. 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 F.

---

<div class="post-metadata">

**Author:** ![PJ\_Hyett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@PJ\_Hyett](https://www.ruby-forum.com/u/PJ_Hyett)\
**Post date:** [February 6, 2014, 12:04pm UTC](https://www.ruby-forum.com/t/explanation-of-if-file-0/55180/5 "2014-02-06T12:04:03Z")

</div>

I agree with Jim F. - I think

if **FILE** == $PROGRAM\_NAME

is slightly more readable than

if **FILE** == $0

Elegance is what keeps me attached to ruby.

---

<div class="post-metadata">

**Author:** ![PJ\_Hyett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@PJ\_Hyett](https://www.ruby-forum.com/u/PJ_Hyett)\
**Post date:** [March 17, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/explanation-of-if-file-0/55180/6 "2006-03-17T19:09:38Z")

</div>

Thanks guys, that’s helpful.

-PJ
