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.