Ruby && OO?

A

aidy

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
 
R

Robert Klemme

Lutz said:
Hi,


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

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

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
 
H

Huw Collingbourne

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.
================================
 
M

Michel

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
 

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

No members online now.

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,675
Latest member
RollandKna

Latest Threads

Top