[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby && OO?

aidy

4/28/2006 8:40:00 AM

Hi,

I have just started learning Ruby as an open-source web test tool makes
use of its language.

If Ruby is object-oriented why can I create a method outside of a class
and why can I implement methods in modules?

Thanks

Aidy

4 Answers

Lutz Horn

4/28/2006 8:46:00 AM

0

Hi,

aidy wrote:
> If Ruby is object-oriented why can I create a method outside of a class
> and why can I implement methods in modules?

Because object-oriented doesn't mean class-oriented but
message-oriented.

http://en.wikipedia.org/wiki/Message_passing_p...

Robert Klemme

4/28/2006 9:28:00 AM

0

Lutz Horn wrote:
> Hi,
>
> aidy wrote:
>> If Ruby is object-oriented why can I create a method outside of a class
>> and why can I implement methods in modules?
>
> Because object-oriented doesn't mean class-oriented but
> message-oriented.
>
> http://en.wikipedia.org/wiki/Message_passing_p...

Yep, OO does not necessarily mean that there are classes at all.

Adding to that: at every single line in the program self has a valid
value, IOW there is always an object around. The fact that you can do

def some_method() end

on top level is just syntactic sugar for a more verbose definition that
would explicitly name the receiver of the definition:

11:23:10 [~]: ruby -e 'def mmmm() end; puts private_methods.grep(/^m+$/)'
mmmm

Kind regards

robert

Huw Collingbourne

4/28/2006 10:04:00 AM

0


"aidy" <aidy.rutter@gmail.com> wrote in message
news:1146213629.494962.42540@j73g2000cwa.googlegroups.com...

> If Ruby is object-oriented why can I create a method outside of a class
> and why can I implement methods in modules?

The answer is that you can't make a method outside of a class. In Ruby a
module is an instance of the class Module. Moreover, when you create a
program, a main Object is automatically created to contain your methods.

Try running this:

puts( self )

puts( self.class )

best wishes
Huw Collingbourne
================================
Bitwise Magazine
www.bitwisemag.com
Dark Neon Ltd.
================================








Michel

4/28/2006 11:14:00 PM

0


> If Ruby is object-oriented why can I create a method outside of a class
> and why can I implement methods in modules?

You are always at least in the context of Object, try:

print self.class