Inheriting from Object

M

Mike Stephens

I'm just working through Ruby Visual Quickstart Guide by Larry Ullman,
which is quite an enjoyable book (so far). In it he has this :

"Methods defined outside of any class ... are automatically defined as
part of the Object class. Because every class automatically inherits
from the Object class, these methods are therefore inherited by every
class."

When I tried this, it didn't work (ie write some methods and then
declare a class and try and use those methods on new members of that
class) . Ruby issues an error message - "private error
called...(nomethoderror)".

I lodged a question on Larry's web site, and to his credit he replied in
due course. He said "I'm sorry: what I wrote probably isn't clear there.
Every object inherits the methods in the original Object, but they don't
inherit methods created in situations like this. Sorry for the
confusion!"

It raises an interesting question. When you write methods outside of a
specific class block, you are in the envelope of some top level class,
so why aren't these methods added?
 
T

Tobias Cohen

"Methods defined outside of any class ... are automatically defined as
part of the Object class. Because every class automatically inherits
from the Object class, these methods are therefore inherited by every
class."

When I tried this, it didn't work (ie write some methods and then
declare a class and try and use those methods on new members of that
class) . Ruby issues an error message - "private error
called...(nomethoderror)".

It looks like these methods are defined as /private/ methods of
Object, so you can't call them from outside of the object. For some
reason, irb confusingly makes public methods instead, so I had to
write a test program to find this out.
It raises an interesting question. When you write methods outside of a
specific class block, you are in the envelope of some top level class,
so why aren't these methods added?

You are in the envelope of the "main" class, but Ruby has a special
case for methods defined here - this thread is fairly informative:

http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/7bb7b451a8f3cca7/98c4c62127b9d945

Tobias Cohen
http://tobiascohen.com/
 
P

pharrington

I'm just working through Ruby Visual Quickstart Guide by Larry Ullman,
which is quite an enjoyable book (so far). In it he has this :

"Methods defined outside of any class ... are automatically defined as
part of the Object class. Because every class automatically inherits
from the Object class, these methods are therefore inherited by every
class."

When I tried this, it didn't work (ie write some methods and then
declare a class and try and use those methods on new members of that
class) . Ruby issues an error message - "private error
called...(nomethoderror)".

I lodged a question on Larry's web site, and to his credit he replied in
due course. He said "I'm sorry: what I wrote probably isn't clear there.
Every object inherits the methods in the original Object, but they don't
inherit methods created in situations like this. Sorry for the
confusion!"

It raises an interesting question. When you write methods outside of a
specific class block, you are in the envelope of some top level class,
so why aren't these methods added?

xeno@Clover:~/projects$ ruby -e "puts class << self; self;
end.inspect"
#<Class:#<Object:0x7f3d0edf6298>>

This says to me that top level is instance of class Object (which
mixes in Kernel), so methods defined in top level will be added to
that object's singleton class, not the base Object class. However this
is just a guess inspired by your question; I await the actual answer
to this.
 
P

pharrington

xeno@Clover:~/projects$ ruby -e "puts class << self; self;
end.inspect"
#<Class:#<Object:0x7f3d0edf6298>>

This says to me that top level is instance of class Object (which
mixes in Kernel), so methods defined in top level will be added to
that object's singleton class, not the base Object class. However this
is just a guess inspired by your question; I await the actual answer
to this.

Ignore all this: I don't understand Ruby.
 
F

Florian Frank

Mike said:
It raises an interesting question. When you write methods outside of a
specific class block, you are in the envelope of some top level class,
so why aren't these methods added?
They are added, they are just private. You have to call them as private
methods. They are made private because they simulate what functions are
used for in other languages. You can call them from anywhere but only
without a receiver. If you want to create methods for classes, that are
supposed to be called on receivers, it's better to inherit from a class,
write a module and include it in your class, or even reopen the class
and add the methods directly. I always thought it was pretty neat how
consistently Ruby uses its OOP design to implement all the features
people have come to expect from modern languages.
 

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
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top