[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Is main an object or a method?

Chad Perrin

4/15/2007 11:58:00 PM

I was surprised, while reading Satish Talim's ruby tutorial, to discover
that main is actually a method -- at least, it is according to the Open
Classes section of the tutorial:

http://rubylearning.com/satishtalim/ruby_open_cl...

To quote:

Please note that self.class refers to Object and self refers to method
main. Therefore while running this program you are executing the main
method of object Object.

Is that correct? I would have thought main was an instance of Object,
and thus was an object itself, not a method of class Object. Using
Object.methods within irb doesn't list a main method, so I'm skeptical.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
Brian K. Reid: "In computer science, we stand on each other's feet."

5 Answers

John Joyce

4/16/2007 1:15:00 AM

0

indeed, main is a method of class Thread
check it with ri
ri main
gives you
Thread.main
which returns the main thread for the process.


self is the current object, a synonym if you will.
Defined as the receiver object of the current method.



On Apr 16, 2007, at 8:57 AM, Chad Perrin wrote:

> I was surprised, while reading Satish Talim's ruby tutorial, to
> discover
> that main is actually a method -- at least, it is according to the
> Open
> Classes section of the tutorial:
>
> http://rubylearning.com/satishtalim/ruby_open_cl...
>
> To quote:
>
> Please note that self.class refers to Object and self refers to
> method
> main. Therefore while running this program you are executing the
> main
> method of object Object.
>
> Is that correct? I would have thought main was an instance of Object,
> and thus was an object itself, not a method of class Object. Using
> Object.methods within irb doesn't list a main method, so I'm
> skeptical.
>
> --
> CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
> Brian K. Reid: "In computer science, we stand on each other's feet."
>


Roseanne Zhang

4/16/2007 2:05:00 AM

0

Chad Perrin wrote:
> I was surprised, while reading Satish Talim's ruby tutorial, to discover

[code]
E:\ex>ruby -e "puts self.kind_of?(Class)"
false

E:\ex>ruby -e "puts self.kind_of?(Module)"
false

E:\ex>ruby -e "puts self.kind_of?(Object)"
true

E:\ex>ruby -e "puts self"
main

E:\ex>ruby -e "puts self.class"
Object
[/code]



--
Posted via http://www.ruby-....

Satish Talim

4/16/2007 2:14:00 AM

0

On Apr 16, 4:57 am, Chad Perrin <per...@apotheon.com> wrote:
> I was surprised, while reading Satish Talim's ruby tutorial, to discover
> that main is actually a method -- at least, it is according to the Open
> Classes section of the tutorial:
>
> http://rubylearning.com/satishtalim/ruby_open_cl...
>
> To quote:
>
> Please note that self.class refers to Object and self refers to method
> main. Therefore while running this program you are executing the main
> method of object Object.
>
> Is that correct? I would have thought main was an instance of Object,
> and thus was an object itself, not a method of class Object. Using
> Object.methods within irb doesn't list a main method, so I'm skeptical.
>
> --
> CCD CopyWrite Chad Perrin [http://ccd.ap...]
> Brian K. Reid: "In computer science, we stand on each other's feet."

Hi Chad,

I stand corrected.

That was the impression I was carrying when I started learning Ruby
(and coming from a long Java background). I realized my folly a long
time back and corrected my statement here -
http://sitekreator.com/satishtalim/first_pr...

but forgot to correct the same at the url you mention. Thanks for
pointing it out - it's now corrected.

Satish

Chad Perrin

4/16/2007 4:21:00 AM

0

On Mon, Apr 16, 2007 at 10:14:49AM +0900, John Joyce wrote:
> indeed, main is a method of class Thread
> check it with ri
> ri main
> gives you
> Thread.main
> which returns the main thread for the process.
>
> self is the current object, a synonym if you will.
> Defined as the receiver object of the current method.

Roseanne's evidence seems to corroborate my impression -- that main (in
the context of the main scope of a Ruby program) is an object of type
Object, not a method. I believe the Thread.main method is a method that
just happens to have the same name as the main instance of Object.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
"The ability to quote is a serviceable
substitute for wit." - W. Somerset Maugham

Chad Perrin

4/16/2007 4:23:00 AM

0

On Mon, Apr 16, 2007 at 11:15:10AM +0900, smtalim wrote:
> On Apr 16, 4:57 am, Chad Perrin <per...@apotheon.com> wrote:
> > I was surprised, while reading Satish Talim's ruby tutorial, to discover
> > that main is actually a method -- at least, it is according to the Open
> > Classes section of the tutorial:
> >
> > http://rubylearning.com/satishtalim/ruby_open_cl...
> >
> > To quote:
> >
> > Please note that self.class refers to Object and self refers to method
> > main. Therefore while running this program you are executing the main
> > method of object Object.
> >
> > Is that correct? I would have thought main was an instance of Object,
> > and thus was an object itself, not a method of class Object. Using
> > Object.methods within irb doesn't list a main method, so I'm skeptical.
> >
> > --
> > CCD CopyWrite Chad Perrin [http://ccd.ap...]
> > Brian K. Reid: "In computer science, we stand on each other's feet."
>
> Hi Chad,
>
> I stand corrected.
>
> That was the impression I was carrying when I started learning Ruby
> (and coming from a long Java background). I realized my folly a long
> time back and corrected my statement here -
> http://sitekreator.com/satishtalim/first_pr...
>
> but forgot to correct the same at the url you mention. Thanks for
> pointing it out - it's now corrected.

I'm glad I could be of help, then. Thanks for the tutorial, by the way.
I find that, even when I'm experienced with a given language beyond the
target audience of a given tutorial, every halfway decent programming
tutorial has something to teach me -- and yours has helped.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
Amazon.com interview candidate: "When C++ is your
hammer, everything starts to look like your thumb."