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

**URL:** <https://www.ruby-forum.com/t/how-to-know-if-i-am-in-a-module-or-in-the-main-script/74931>\
**Category:** Ruby\
**Created:** [November 7, 2006, 11:02am UTC](https://www.ruby-forum.com/t/how-to-know-if-i-am-in-a-module-or-in-the-main-script/74931 "2006-11-07T11:02:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [November 7, 2006, 11:02am UTC](https://www.ruby-forum.com/t/how-to-know-if-i-am-in-a-module-or-in-the-main-script/74931/1 "2006-11-07T11:02:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [November 7, 2006, 11:02am UTC](https://www.ruby-forum.com/t/how-to-know-if-i-am-in-a-module-or-in-the-main-script/74931/2 "2006-11-07T11:02:55Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [November 7, 2006, 11:03am UTC](https://www.ruby-forum.com/t/how-to-know-if-i-am-in-a-module-or-in-the-main-script/74931/3 "2006-11-07T11:03:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [November 7, 2006, 11:03am UTC](https://www.ruby-forum.com/t/how-to-know-if-i-am-in-a-module-or-in-the-main-script/74931/4 "2006-11-07T11:03:33Z")

</div>

Phrogz wrote:

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

---

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [November 7, 2006, 11:03am UTC](https://www.ruby-forum.com/t/how-to-know-if-i-am-in-a-module-or-in-the-main-script/74931/5 "2006-11-07T11:03:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [November 7, 2006, 11:03am UTC](https://www.ruby-forum.com/t/how-to-know-if-i-am-in-a-module-or-in-the-main-script/74931/6 "2006-11-07T11:03:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [November 7, 2006, 11:04am UTC](https://www.ruby-forum.com/t/how-to-know-if-i-am-in-a-module-or-in-the-main-script/74931/7 "2006-11-07T11:04:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [November 7, 2006, 11:03am UTC](https://www.ruby-forum.com/t/how-to-know-if-i-am-in-a-module-or-in-the-main-script/74931/8 "2006-11-07T11:03:58Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![matrixise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@matrixise](https://www.ruby-forum.com/u/matrixise)\
**Post date:** [November 7, 2006, 11:08am UTC](https://www.ruby-forum.com/t/how-to-know-if-i-am-in-a-module-or-in-the-main-script/74931/9 "2006-11-07T11:08:12Z")

</div>

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
