how to know if i am in a module or in the main script ?

  • Thread starter Stephane Wirtel
  • Start date
S

Stephane Wirtel

Hi all,

with ruby, it's possible to check if I am in the main script or in a
module with if __name__ == 'main'

Is there the same thing with ruby ?

Thanks
 
S

Stephane Wirtel

Stephane said:
Hi all,

with ruby, it's possible to check if I am in the main script or in a
module with if __name__ == 'main'

Is there the same thing with ruby ?

Thanks
Here is a way to find if a ruby file is a module or the main script.

if $0 == __FILE__
puts "This is the main function"
else
puts "This is a module"
end

Is it right ?
 
P

Phrogz

Stephane said:
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. :)
 
D

David Vallner

--------------enig0F3BC2BE03B29EA22DF7C96F
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
=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--
 
J

Jeremy McAnally

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
 
D

David Vallner

--------------enig788235182AB01E4916B108FF
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Jeremy said:
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.
=20

By cluttering the file internally with code not related to its main
purpose? I still claim laziness being the motive to not separate these
things, half-decent code browsing and project management tools / project
structuring conventions will negate any problems with finding code in
files anyway.

Either way, YMMV. I'll concede that this is more of a religious than
technical issue for me, stemming from my preference to separate concerns
as tidily as possible.

David Vallner


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

iD8DBQFFTgySy6MhrS8astoRAm5tAJ9FWnG6ijVH46+cQ2MELrTd7xid4gCfX0Fi
US0T5Q1AGiLB/XO29uPXDUw=
=71X8
-----END PGP SIGNATURE-----

--------------enig788235182AB01E4916B108FF--
 
S

Stephane Wirtel

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. :)
Thanks
 
J

James Edward Gray II

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.

This is a good point in that this in not Ruby specific. I've seen
this idiom in many languages for years now.

I like it and often use it to put a basic command-line interface over
my libraries.

James Edward Gray II
 
T

Tom Pollard

By cluttering the file internally with code not related to its main
purpose? I still claim laziness being the motive to not separate these
things,

Ah, but is laziness not one of the cardinal virtues?

Either way, YMMV. I'll concede that this is more of a religious than
technical issue for me, stemming from my preference to separate
concerns
as tidily as possible.

"Separating concerns" sounds like a good principle, but I'd describe
keeping test code in the library source file as "keeping related
things together", which is an equally good principle. Now, in many
cases it may be better, or even necessary, to keep tests separate
from the code, but I think it makes a lot of sense to keep basic
functionality tests in same source file as the module they're
testing. I also first encountered this style in the Python
community, and have come to like it. I especially like using this
style when I first start writing a new module; later, when my modules
become more mature and the tests get longer, I might migrate them out
to separate files. It makes me happy to learn that Ruby supports
this as well. (I don't think Perl does.)


TomP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,218
Messages
2,571,123
Members
47,725
Latest member
Rudy

Latest Threads

Top