# Converting Python to Ruby - question about main()

**URL:** <https://www.ruby-forum.com/t/converting-python-to-ruby-question-about-main/92555>\
**Category:** Ruby\
**Created:** [April 20, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/converting-python-to-ruby-question-about-main/92555 "2007-04-20T17:39:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [April 20, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/converting-python-to-ruby-question-about-main/92555/1 "2007-04-20T17:39:27Z")

</div>

Some Python programs have a couple lines of code like this:

if **main** == “ **main** ”  
main()

I think it’s supposed to call the main function if the script is  
executed  
directly. Is there an equivalent in Ruby? Or is it even necessary in  
Ruby?

(I’m new to Ruby, so excuse me if this is a stupid question.)

Mike S.

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [April 20, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/converting-python-to-ruby-question-about-main/92555/2 "2007-04-20T17:47:28Z")

</div>

On Apr 20, 2007, at 11:39 AM, Mike S. wrote:

> (I’m new to Ruby, so excuse me if this is a stupid question.)
> 
> Mike S.

I’m not entirely sure what that Python code means, but to do  
something if the script is executed directly, you can try:

if **FILE** == $0  
(do something)  
end

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [April 20, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/converting-python-to-ruby-question-about-main/92555/3 "2007-04-20T20:17:44Z")

</div>

These two snippets of code do the exact same thing: execute code if this  
is  
the source file being directly executed and not required (ruby) or  
imported  
(python).

Jason

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [April 20, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/converting-python-to-ruby-question-about-main/92555/4 "2007-04-20T20:41:46Z")

</div>

On 4/20/07, Jason R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Some Python programs have a couple lines of code like this:
> > > 
> > > if **main** == “ **main** ”

I’m guessing the original poster probably meant this:

if **name** == ‘ **main** ’:
