# If \_\_FILE\_ == $0 executed twice

**URL:** <https://rubytalk.org/t/if-file-0-executed-twice/18595>\
**Category:** ruby-talk\
**Created:** [24 May 2005 09:19 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595 "2005-05-24T09:19:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Han\_Holl](https://avatars.discourse-cdn.com/v4/letter/h/d2c977/32.png) [@Han\_Holl](https://rubytalk.org/u/Han_Holl)\
**Post date:** [24 May 2005 09:19 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/1 "2005-05-24T09:19:37Z")

</div>

Hello,

As recommended in the pickaxe I often have a section at the end of a  
.rb file with  
if \_\_FILE\_\_ == $0  
....  
end

However, if one of the included files includes this file itself (not  
unlikely at all), the code gets executed twice.  
Is there a reasonable workaround for this ?

Cheers,

Han Holl

---

<div class="post-metadata">

**Author:** ![Han\_Holl](https://avatars.discourse-cdn.com/v4/letter/h/d2c977/32.png) [@Han\_Holl](https://rubytalk.org/u/Han_Holl)\
**Post date:** [24 May 2005 09:33 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/2 "2005-05-24T09:33:51Z")

</div>

My apologies for answering myself, but I've discovered that  
if \_\_FILE\_\_ == $0 and caller(0).size == 1  
does what I want.

More elegant suggestions remain welcome of course.

Cheers,

Han Holl

> **···**
>
> On 5/24/05, Han Holl \<han.holl@gmail.com\> wrote:
> 
> > Hello,
> > 
> > As recommended in the pickaxe I often have a section at the end of a  
> > .rb file with  
> > if \_\_FILE\_\_ == $0  
> > ....  
> > end
> > 
> > However, if one of the included files includes this file itself (not  
> > unlikely at all), the code gets executed twice.  
> > Is there a reasonable workaround for this ?
> > 
> > Cheers,
> > 
> > Han Holl

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [24 May 2005 12:38 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/3 "2005-05-24T12:38:48Z")

</div>

Are you using 'load' or 'require'?

> **···**
>
> On May 24, 2005, at 3:19 AM, Han Holl wrote:
> 
> > However, if one of the included files includes this file itself (not  
> > unlikely at all), the code gets executed twice.  
> > Is there a reasonable workaround for this ?

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [24 May 2005 14:20 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/4 "2005-05-24T14:20:37Z")

</div>

i have code generators for 'serious' lib files that do this:

&nbsp;&nbsp;&nbsp;unless defined? $\_\_foo\_bar\_\_  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module Foo  
&nbsp;&nbsp;&nbsp;#--{{{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Foo::LIBDIR = File::dirname(File::expand\_path(\_\_FILE\_\_)) + File::SEPARATOR unless  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defined? Foo::LIBDIR

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Bar  
&nbsp;&nbsp;&nbsp;#--{{{  
&nbsp;&nbsp;&nbsp;#--}}}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end # class Bar  
&nbsp;&nbsp;&nbsp;#--}}}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end # module Foo  
&nbsp;&nbsp;&nbsp;$\_\_foo\_bar\_\_ = \_\_FILE\_\_  
&nbsp;&nbsp;&nbsp;end

it's the 'unless defined?' bit that's imortant here, so something like:

&nbsp;&nbsp;&nbsp;unless defined? $\_\_foo\_main\_\_

&nbsp;&nbsp;&nbsp;module Foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Main  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;Foo::Main::new(ARGV).run if $0 == \_\_FILE\_\_

&nbsp;&nbsp;&nbsp;$\_\_foo\_main\_\_ = \_\_FILE\_\_  
&nbsp;&nbsp;&nbsp;end

will do the trick. the reason it happens is that

&nbsp;&nbsp;&nbsp;require 'a.rb' # loads file

and

&nbsp;&nbsp;&nbsp;require './a.rb' # loads file

because require doesn't do a File::expand\_path on loaded features. imho it's  
a bug and require should actually hash the stat of the file to see if it's  
loaded but that explodes on nfs sometimes... at least expanding the path would  
help though.

cheers.

-a

> **···**
>
> On Tue, 24 May 2005, Han Holl wrote:
> 
> > Hello,
> > 
> > As recommended in the pickaxe I often have a section at the end of a  
> > rb file with  
> > if \_\_FILE\_\_ =3D=3D $0  
> > ...  
> > end
> > 
> > However, if one of the included files includes this file itself (not  
> > unlikely at all), the code gets executed twice.  
> > Is there a reasonable workaround for this ?
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > My religion is very simple. My religion is kindness.  
> > --Tenzin Gyatso
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [24 May 2005 19:58 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/5 "2005-05-24T19:58:09Z")

</div>

Hi,

> **···**
>
> Am Dienstag, 24. Mai 2005, 18:19:37 +0900 schrieb Han Holl:
> 
> > As recommended in the pickaxe I often have a section at the end of a  
> > .rb file with  
> > if \_\_FILE\_\_ == $0  
> > ....  
> > end
> > 
> > However, if one of the included files includes this file itself (not  
> > unlikely at all), the code gets executed twice.
> 
> Not here. What is your output of this:
> 
> --------------------  
> def gen prg, oth  
> &nbsp;&nbsp;name = "#{prg}.rb"  
> &nbsp;&nbsp;File.open name, "w" do |f|  
> &nbsp;&nbsp;&nbsp;&nbsp;f.puts \<\<HERE  
> require "#{oth}"
> 
> puts [\_\_FILE\_\_, $0].inspect
> 
> if \_\_FILE\_\_ == $0 then  
> &nbsp;&nbsp;puts "Hello, here is `#{prg}'."  
> end  
> HERE  
> &nbsp;&nbsp;&nbsp;&nbsp;name  
> &nbsp;&nbsp;end  
> end
> 
> names = ["a", "b"]
> 
> a, b = gen( \*names), gen( \*names.reverse)  
> system "ruby #{a}"  
> --------------------
> 
> Here (Debian Linux):
> 
> --------------------  
> ["/home/user/tmp/ruby/a.rb", "a.rb"]  
> ["/home/user/tmp/ruby/b.rb", "a.rb"]  
> ["a.rb", "a.rb"]  
> Hello, here is `a'.  
> --------------------
> 
> Which OS are you working under?
> 
> Bertram
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)

---

<div class="post-metadata">

**Author:** ![Karl\_von\_Laudermann](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@Karl\_von\_Laudermann](https://rubytalk.org/u/Karl_von_Laudermann)\
**Post date:** [24 May 2005 20:45 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/6 "2005-05-24T20:45:17Z")

</div>

Han Holl wrote:

> Hello,
> 
> As recommended in the pickaxe I often have a section at the end of a  
> rb file with  
> if \_\_FILE\_\_ == $0  
> ...  
> end

Out of curiosity, where is this mentioned in the Pickaxe? I don't  
remember seeing it.

---

<div class="post-metadata">

**Author:** ![Han\_Holl](https://avatars.discourse-cdn.com/v4/letter/h/d2c977/32.png) [@Han\_Holl](https://rubytalk.org/u/Han_Holl)\
**Post date:** [24 May 2005 13:34 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/7 "2005-05-24T13:34:08Z")

</div>

require. The initial ruby file does not wind up in $"

Han

> **···**
>
> On 5/24/05, Gavin Kistner \<gavin@refinery.com\> wrote:
> 
> > On May 24, 2005, at 3:19 AM, Han Holl wrote:  
> > \> However, if one of the included files includes this file itself (not  
> > \> unlikely at all), the code gets executed twice.  
> > \> Is there a reasonable workaround for this ?
> > 
> > Are you using 'load' or 'require'?

---

<div class="post-metadata">

**Author:** ![Han\_Holl](https://avatars.discourse-cdn.com/v4/letter/h/d2c977/32.png) [@Han\_Holl](https://rubytalk.org/u/Han_Holl)\
**Post date:** [24 May 2005 14:56 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/8 "2005-05-24T14:56:50Z")

</div>

Yes, I also find this strange. $0 should also be included. Maybe  
require could even hash on a md5sum of the contents of the file ?

Han Holl

> **···**
>
> On 5/24/05, Ara.T.Howard@noaa.gov \<Ara.T.Howard@noaa.gov\> wrote:
> 
> > [cut]  
> > because require doesn't do a File::expand\_path on loaded features. imho it's  
> > a bug and require should actually hash the stat of the file to see if it's  
> > loaded but that explodes on nfs sometimes... at least expanding the path would  
> > help though.

---

<div class="post-metadata">

**Author:** ![Nakada\_Nobuyoshi](https://avatars.discourse-cdn.com/v4/letter/n/848f3c/32.png) [@Nakada\_Nobuyoshi](https://rubytalk.org/u/Nakada_Nobuyoshi)\
**Post date:** [25 May 2005 02:22 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/9 "2005-05-25T02:22:45Z")

</div>

Hi,

At Tue, 24 May 2005 23:20:37 +0900,  
Ara.T.Howard@noaa.gov wrote in [ruby-talk:143540]:

> because require doesn't do a File::expand\_path on loaded features.

1.9 does.

> **···**
>
> --  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Robert\_Peacock](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Robert\_Peacock](https://rubytalk.org/u/Robert_Peacock)\
**Post date:** [25 May 2005 03:48 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/10 "2005-05-25T03:48:58Z")

</div>

Another note along this line...I unintentionally discovered the other day that on Windows require is case sensative while the file system is not. require "Foo" and require "foo" will cause the same file to be executed twice.

> **···**
>
> Ara.T.Howard@noaa.gov wrote:
> 
> > On Tue, 24 May 2005, Han Holl wrote:
> > 
> > > Hello,
> > > 
> > > As recommended in the pickaxe I often have a section at the end of a  
> > > rb file with  
> > > if \_\_FILE\_\_ =3D=3D $0  
> > > ...  
> > > end
> > > 
> > > However, if one of the included files includes this file itself (not  
> > > unlikely at all), the code gets executed twice.  
> > > Is there a reasonable workaround for this ?
> > 
> > i have code generators for 'serious' lib files that do this:
> > 
> > &nbsp;&nbsp;unless defined? $\_\_foo\_bar\_\_  
> > &nbsp;&nbsp;&nbsp;&nbsp;module Foo  
> > &nbsp;&nbsp;#--{{{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Foo::LIBDIR = File::dirname(File::expand\_path(\_\_FILE\_\_)) + File::SEPARATOR unless  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defined? Foo::LIBDIR
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Bar  
> > &nbsp;&nbsp;#--{{{  
> > &nbsp;&nbsp;#--}}}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end # class Bar  
> > &nbsp;&nbsp;#--}}}  
> > &nbsp;&nbsp;&nbsp;&nbsp;end # module Foo  
> > &nbsp;&nbsp;$\_\_foo\_bar\_\_ = \_\_FILE\_\_  
> > &nbsp;&nbsp;end
> > 
> > it's the 'unless defined?' bit that's imortant here, so something like:
> > 
> > &nbsp;&nbsp;unless defined? $\_\_foo\_main\_\_
> > 
> > &nbsp;&nbsp;module Foo  
> > &nbsp;&nbsp;&nbsp;&nbsp;class Main  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;Foo::Main::new(ARGV).run if $0 == \_\_FILE\_\_
> > 
> > &nbsp;&nbsp;$\_\_foo\_main\_\_ = \_\_FILE\_\_  
> > &nbsp;&nbsp;end
> > 
> > will do the trick. the reason it happens is that
> > 
> > &nbsp;&nbsp;require 'a.rb' # loads file
> > 
> > and
> > 
> > &nbsp;&nbsp;require './a.rb' # loads file
> > 
> > because require doesn't do a File::expand\_path on loaded features. imho it's  
> > a bug and require should actually hash the stat of the file to see if it's  
> > loaded but that explodes on nfs sometimes... at least expanding the path would  
> > help though.
> > 
> > cheers.
> > 
> > -a

---

<div class="post-metadata">

**Author:** ![Gene\_Tani](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@Gene\_Tani](https://rubytalk.org/u/Gene_Tani)\
**Post date:** [25 May 2005 05:00 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/11 "2005-05-25T05:00:18Z")

</div>

p. 337

---

<div class="post-metadata">

**Author:** ![Han\_Holl](https://avatars.discourse-cdn.com/v4/letter/h/d2c977/32.png) [@Han\_Holl](https://rubytalk.org/u/Han_Holl)\
**Post date:** [25 May 2005 10:28 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/12 "2005-05-25T10:28:57Z")

</div>

Hi Bertram,

This had my puzzled for a while, until I added '#!/usr/bin/ruby' to  
a.rb, and made it executable.  
Then ./a.rb produced:  
["./a.rb", "./a.rb"]  
Hello, here is `a'.  
["./b.rb", "./a.rb"]  
["./a.rb", "./a.rb"]  
Hello, here is `a'.

Cheers,

Han Holl

> **···**
>
> On 5/24/05, Bertram Scharpf \<lists@bertram-scharpf.de\> wrote:
> 
> > Hi,
> > 
> > Am Dienstag, 24. Mai 2005, 18:19:37 +0900 schrieb Han Holl:  
> > \> As recommended in the pickaxe I often have a section at the end of a  
> > \> .rb file with  
> > \> if \_\_FILE\_\_ == $0  
> > \> ....  
> > \> end  
> > \>  
> > \> However, if one of the included files includes this file itself (not  
> > \> unlikely at all), the code gets executed twice.
> > 
> > Not here. What is your output of this:
> > 
> > --------------------  
> > def gen prg, oth  
> > &nbsp;&nbsp;name = "#{prg}.rb"  
> > &nbsp;&nbsp;File.open name, "w" do |f|  
> > &nbsp;&nbsp;&nbsp;&nbsp;f.puts \<\<HERE  
> > require "#{oth}"
> > 
> > puts [\_\_FILE\_\_, $0].inspect
> > 
> > if \_\_FILE\_\_ == $0 then  
> > &nbsp;&nbsp;puts "Hello, here is `#{prg}'."  
> > end  
> > HERE  
> > &nbsp;&nbsp;&nbsp;&nbsp;name  
> > &nbsp;&nbsp;end  
> > end
> > 
> > names = ["a", "b"]
> > 
> > a, b = gen( \*names), gen( \*names.reverse)  
> > system "ruby #{a}"  
> > --------------------
> > 
> > Here (Debian Linux):
> > 
> > --------------------  
> > ["/home/user/tmp/ruby/a.rb", "a.rb"]  
> > ["/home/user/tmp/ruby/b.rb", "a.rb"]  
> > ["a.rb", "a.rb"]  
> > Hello, here is `a'.  
> > --------------------
> > 
> > Which OS are you working under?
> > 
> > Bertram

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [25 May 2005 02:50 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/13 "2005-05-25T02:50:18Z")

</div>

it just keeps gettin better! 😉

-a

> **···**
>
> On Wed, 25 May 2005, nobuyoshi nakada wrote:
> 
> > Hi,
> > 
> > At Tue, 24 May 2005 23:20:37 +0900,  
> > Ara.T.Howard@noaa.gov wrote in [ruby-talk:143540]:
> > 
> > > because require doesn't do a File::expand\_path on loaded features.
> > 
> > 1.9 does.
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > My religion is very simple. My religion is kindness.  
> > --Tenzin Gyatso
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Han\_Holl](https://avatars.discourse-cdn.com/v4/letter/h/d2c977/32.png) [@Han\_Holl](https://rubytalk.org/u/Han_Holl)\
**Post date:** [25 May 2005 10:51 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/14 "2005-05-25T10:51:14Z")

</div>

Does it also put $0 in $" ?  
That would solve my problem.

Han Holl

> **···**
>
> On 5/25/05, nobuyoshi nakada \<nobuyoshi.nakada@ge.com\> wrote:
> 
> > Hi,
> > 
> > At Tue, 24 May 2005 23:20:37 +0900,  
> > Ara.T.Howard@noaa.gov wrote in [ruby-talk:143540]:  
> > \> because require doesn't do a File::expand\_path on loaded features.
> > 
> > 1.9 does.

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [26 May 2005 04:03 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/15 "2005-05-26T04:03:54Z")

</div>

Hi,

> **···**
>
> Am Mittwoch, 25. Mai 2005, 19:28:57 +0900 schrieb Han Holl:
> 
> > On 5/24/05, Bertram Scharpf \<lists@bertram-scharpf.de\> wrote:  
> > \> Am Dienstag, 24. Mai 2005, 18:19:37 +0900 schrieb Han Holl:  
> > \> \> As recommended in the pickaxe I often have a section at the end of a  
> > \> \> .rb file with  
> > \> \> if \_\_FILE\_\_ == $0  
> > \> \> ....  
> > \> \> end  
> > \> \>  
> > \> Hello, here is `a'.  
> > \> --------------------  
> > \> Which OS are you working under?
> > 
> > This had my puzzled for a while, until I added '#!/usr/bin/ruby' to  
> > a.rb, and made it executable.  
> > Then ./a.rb produced:  
> > ["./a.rb", "./a.rb"]  
> > Hello, here is `a'.  
> > ["./b.rb", "./a.rb"]  
> > ["./a.rb", "./a.rb"]  
> > Hello, here is `a'.
> 
> Still not here. Under which circumstances do you execute?  
> Me: 1.8.2 apt and 1.9 self-compiled, Debian.
> 
> Just curious.
> 
> Bertram
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)

---

<div class="post-metadata">

**Author:** ![Michal\_hramrach\_Such](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@Michal\_hramrach\_Such](https://rubytalk.org/u/Michal_hramrach_Such)\
**Post date:** [26 May 2005 13:43 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/16 "2005-05-26T13:43:23Z")

</div>

> Another note along this line...I unintentionally discovered the other day that on Windows require is case sensative while the file system is not. require "Foo" and require "foo" will

..unless you turn on case sensitivity..

> cause the same file to be executed twice.

Thanks

Michal Suchanek

> **···**
>
> On 25. May 2005, at 5:48, Robert Peacock wrote:

---

<div class="post-metadata">

**Author:** ![Nakada\_Nobuyoshi](https://avatars.discourse-cdn.com/v4/letter/n/848f3c/32.png) [@Nakada\_Nobuyoshi](https://rubytalk.org/u/Nakada_Nobuyoshi)\
**Post date:** [25 May 2005 10:58 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/17 "2005-05-25T10:58:04Z")

</div>

Hi,

At Wed, 25 May 2005 19:51:14 +0900,  
Han Holl wrote in [ruby-talk:143604]:

> Does it also put $0 in $" ?

No, $0 isn't required.

> That would solve my problem.

Why not move the `if' block to another file (with no suffix), like  
irb for instance?

> **···**
>
> --  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Han\_Holl](https://avatars.discourse-cdn.com/v4/letter/h/d2c977/32.png) [@Han\_Holl](https://rubytalk.org/u/Han_Holl)\
**Post date:** [26 May 2005 07:30 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/18 "2005-05-26T07:30:24Z")

</div>

I don't understand that at all. Both on FC3 (standard FC3 ruby) and on  
RH9 (ruby-1.8.2-26 from Ian Macdonald) I get 'Hello, here is `a'.'  
twice.  
It makes a difference whether you run 'ruby a.rb' or './a.rb'.

Cheers,

Han Holl

> **···**
>
> On 5/26/05, Bertram Scharpf \<lists@bertram-scharpf.de\> wrote:
> 
> > Still not here. Under which circumstances do you execute?  
> > Me: 1.8.2 apt and 1.9 self-compiled, Debian.
> > 
> > Just curious.
> > 
> > Bertram

---

<div class="post-metadata">

**Author:** ![Han\_Holl](https://avatars.discourse-cdn.com/v4/letter/h/d2c977/32.png) [@Han\_Holl](https://rubytalk.org/u/Han_Holl)\
**Post date:** [25 May 2005 12:55 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/19 "2005-05-25T12:55:39Z")

</div>

Under the if are mostly tests. The files are meant as libraries. It's  
nice to have the tests nearby, and not in a seperate file.

Is there a reason why $0 is not required ? It seems a logical thing to do.  
Would it break things ?  
Just curious.

Cheers

Han Holl

> **···**
>
> On 5/25/05, nobuyoshi nakada \<nobuyoshi.nakada@ge.com\> wrote:
> 
> > Hi,
> > 
> > At Wed, 25 May 2005 19:51:14 +0900,  
> > Han Holl wrote in [ruby-talk:143604]:  
> > \> Does it also put $0 in $" ?
> > 
> > No, $0 isn't required.
> > 
> > \> That would solve my problem.
> > 
> > Why not move the `if' block to another file (with no suffix), like  
> > irb for instance?

---

<div class="post-metadata">

**Author:** ![Nakada\_Nobuyoshi](https://avatars.discourse-cdn.com/v4/letter/n/848f3c/32.png) [@Nakada\_Nobuyoshi](https://rubytalk.org/u/Nakada_Nobuyoshi)\
**Post date:** [26 May 2005 06:23 UTC](https://rubytalk.org/t/if-file-0-executed-twice/18595/20 "2005-05-26T06:23:01Z")

</div>

Hi,

At Wed, 25 May 2005 21:55:39 +0900,  
Han Holl wrote in [ruby-talk:143610]:

> \> Why not move the `if' block to another file (with no suffix), like  
> \> irb for instance?  
> \>  
> Under the if are mostly tests. The files are meant as libraries. It's  
> nice to have the tests nearby, and not in a seperate file.

Another way is put the tests after \_\_END\_\_ line. See  
InlineTest#loadtest\_\_END\_\_part in test/inlinetest.rb.

> Is there a reason why $0 is not required ? It seems a logical thing to do.  
> Would it break things ?

Till 1.8, $: includes `require'd names relative to one of $LOAD\_PATH,  
$0 is absolute path or relative from cwd in other hand. So comparing  
$0 with $: had no meanings, however it might be in 1.9.

> **···**
>
> --  
> Nobu Nakada

[Next page](https://rubytalk.org/t/if-file-0-executed-twice/18595.md?page=2)
