Converting Python to Ruby - question about main()

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 Steiner

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

···

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

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 Steiner

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

···

On 4/20/07, Roland Crosby <roland.crosby@students.olin.edu> wrote:

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

> 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 Steiner

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

I'm guessing the original poster probably meant this:

if __name__ == '__main__':

···

On 4/20/07, Jason Roelofs <jameskilton@gmail.com> wrote:

> > Some Python programs have a couple lines of code like this:
> >
> > if __main__ == "__main__"

--
Greg Donald
http://destiney.com/