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

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

Stephane W. wrote:

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 ?

Stephane W. 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. :slight_smile:

Phrogz wrote:

determine if the code is being included or ‘run’ by itself. Good job
figuring it out. :slight_smile:

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 V.

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

Jeremy McAnally wrote:

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.

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 V.

On Nov 5, 2006, at 8:30 AM, Jeremy McAnally wrote:

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 G. II

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. :slight_smile:
Thanks

On Nov 5, 2006, at 11:08 AM, David V. wrote:

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