[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

if __FILE_ == $0 executed twice

Han Holl

5/24/2005 9:20:00 AM

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


20 Answers

Han Holl

5/24/2005 9:34:00 AM

0

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
>


Gavin Kistner

5/24/2005 12:39:00 PM

0

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'?

Han Holl

5/24/2005 1:34:00 PM

0

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'?
>
>
require. The initial ruby file does not wind up in $"

Han


Ara.T.Howard

5/24/2005 2:06:00 PM

0

Han Holl

5/24/2005 2:57:00 PM

0

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.
>
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


Bertram Scharpf

5/24/2005 7:58:00 PM

0

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
name = "#{prg}.rb"
File.open name, "w" do |f|
f.puts <<HERE
require "#{oth}"

puts [ __FILE__, $0].inspect

if __FILE__ == $0 then
puts "Hello, here is `#{prg}'."
end
HERE
name
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-...


Karl von Laudermann

5/24/2005 8:42:00 PM

0

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.

Nakada, Nobuyoshi

5/25/2005 2:23:00 AM

0

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


Ara.T.Howard

5/25/2005 2:36:00 AM

0

Robert Peacock

5/25/2005 3:49:00 AM

0

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:
>
> unless defined? $__foo_bar__
> module Foo
> #--{{{
> Foo::LIBDIR = File::dirname(File::expand_path(__FILE__)) +
> File::SEPARATOR unless
> defined? Foo::LIBDIR
>
> class Bar
> #--{{{
> #--}}}
> end # class Bar
> #--}}}
> end # module Foo
> $__foo_bar__ = __FILE__
> end
>
>
> it's the 'unless defined?' bit that's imortant here, so something like:
>
> unless defined? $__foo_main__
>
> module Foo
> class Main
> end
> end
>
> Foo::Main::new(ARGV).run if $0 == __FILE__
>
> $__foo_main__ = __FILE__
> end
>
> will do the trick. the reason it happens is that
>
> require 'a.rb' # loads file
>
> and
>
> 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

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.