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