I don't know about Ruby, but many times this is used in Python libs to
run example code if it's being run by itself and just be a library if
it's included.  Pretty neat way of keeping the file clutter to a
minimum.

--Jeremy

On 11/5/06, David Vallner <david / vallner.net> wrote:
> Phrogz wrote:
> > Stephane Wirtel wrote:
> >> if $0 == __FILE__
> >>   puts "This is the main function"
> >> else
> >>   puts "This is a module"
> >> end
> >>
> >> Is it right ?
> >
> > Yes, that is a very common idiom used by many Ruby programmers to
> > determine if the code is being included or 'run' by itself. Good job
> > figuring it out. :)
> >
> >
>
> Coincidentally, an idiom I don't like. Let libraries be libraries,
> executables be executables (I prefer to use minimal "wire up" scripts
> for my standalone apps, and keep the reusable bits in other files), and
> test code be test code. Having a library test itself when "run" is a
> common kludge I find rather pointless - not like your users ever need to
> know test run results, it's the developer's responsibility to take care
> of those. And it's the job of development tools to handle running tests
> conveniently, not the programmer's by making his code indirectly depend
> on its tests. (IIRC some RubyMate screencasts, that editor does seem to
> have this thing covered.)
>
> David Vallner
>
>
>
>