# Inheriting from Object

**URL:** <https://www.ruby-forum.com/t/inheriting-from-object/179173>\
**Category:** Ruby\
**Created:** [November 19, 2009, 12:14am UTC](https://www.ruby-forum.com/t/inheriting-from-object/179173 "2009-11-19T00:14:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [November 19, 2009, 12:14am UTC](https://www.ruby-forum.com/t/inheriting-from-object/179173/1 "2009-11-19T00:14:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [November 19, 2009, 12:38am UTC](https://www.ruby-forum.com/t/inheriting-from-object/179173/2 "2009-11-19T00:38:17Z")

</div>

On 19/11/2009, at 10:16 AM, Mike S. wrote:

> “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](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/7bb7b451a8f3cca7/98c4c62127b9d945)

Tobias Cohen

> **[Tobias Cohen](https://tobico.net/)**
>
> Web + game developer

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [November 19, 2009, 12:45am UTC](https://www.ruby-forum.com/t/inheriting-from-object/179173/3 "2009-11-19T00:45:53Z")

</div>

On Nov 18, 6:16 pm, Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## class) . Ruby issues an error message - "private error so why aren’t these methods added?
> 
> Posted viahttp://www.ruby-forum.com/.

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.

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [November 19, 2009, 12:46pm UTC](https://www.ruby-forum.com/t/inheriting-from-object/179173/4 "2009-11-19T12:46:56Z")

</div>

Mike S. wrote:

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

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [November 19, 2009, 12:50am UTC](https://www.ruby-forum.com/t/inheriting-from-object/179173/5 "2009-11-19T00:50:34Z")

</div>

On Nov 18, 6:43 pm, pharrington [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > part of the Object class. Because every class automatically inherits  
> > Every object inherits the methods in the original Object, but they don’t  
> > 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.
