From: David Vallner Date: 2006-11-05T21:26:29+09:00 Subject: Re: how to know if i am in a module or in the main script ? --------------enig0F3BC2BE03B29EA22DF7C96F Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Phrogz wrote: > Stephane Wirtel wrote: >> if $0 =3D=3D __FILE__ >> puts "This is the main function" >> else >> puts "This is a module" >> end >> >> Is it right ? >=20 > 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. :) >=20 >=20 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 --------------enig0F3BC2BE03B29EA22DF7C96F Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (MingW32) iD8DBQFFTdhsy6MhrS8astoRAndnAJ9nIzBkPc3PmrOh5eMNoIgG7mkEygCfbP4Z YOXhKpo1MmVD1rqY+sG5TMM= =ovMc -----END PGP SIGNATURE----- --------------enig0F3BC2BE03B29EA22DF7C96F--