Is main an object or a method?

C

Chad Perrin

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_classes.html

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

John Joyce

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

Roseanne Zhang

Chad said:
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
 
S

smtalim

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_classes.html

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.

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_program.html

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

Satish
 
C

Chad Perrin

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

Chad Perrin

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_program.html

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top