From: Jeremy McAnally Date: 2006-11-05T23:30:26+09:00 Subject: Re: how to know if i am in a module or in the main script ? 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 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 > > > >