# That top-level 'self'

**URL:** <https://www.ruby-forum.com/t/that-top-level-self/119690>\
**Category:** Ruby\
**Created:** [November 9, 2007, 8:06am UTC](https://www.ruby-forum.com/t/that-top-level-self/119690 "2007-11-09T08:06:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 9, 2007, 8:06am UTC](https://www.ruby-forum.com/t/that-top-level-self/119690/1 "2007-11-09T08:06:29Z")

</div>

The ruby top-level ‘self’ seems like a strange beast. It is an instance  
of Object. But definitions of methods, modules, classes, and constants  
magically go into the Object class. (So say the reflection methods.)

(In “irb” the methods are public. In “ruby” they are private.)

Can this be understood as less magical?

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 9, 2007, 8:25am UTC](https://www.ruby-forum.com/t/that-top-level-self/119690/2 "2007-11-09T08:25:54Z")

</div>

On Nov 9, 2:06 am, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The ruby top-level ‘self’ seems like a strange beast. It is an instance  
> of Object. But definitions of methods, modules, classes, and constants  
> magically go into the Object class. (So say the reflection methods.)  
> (In “irb” the methods are public. In “ruby” they are private.)
> 
> Can this be understood as less magical?

I doubt it. ‘main’ as it is called, delegates to Object. And it is not  
a complete delegation either. Try this…

define\_method(:a){}

and you will get a method missing error. Indeed I’ve run into this  
before and created a fix:

require ‘facets/main’

It seems silly to have to do this. It’ makes more sense to me for main  
to be a self extended module instead. the it would not be magical.  
While in some respects it may seem convenient to have all top-level  
methods wind up in every object, it limits Ruby. For instance, if we  
want to use Ruby for quick system scripting, we will be writing a lot  
of top-level methods. And funny things can crop up.

require ‘open-uri’  
def meta; nil; end  
open(‘[http://google.com](http://google.com)’)

NoMethodError: undefined method `status=’ for #StringIO:0xb7a6c374

No doubt this is due to a poor piece of code in open-uri, but it would  
never have been raised here if it weren’t for top-level method  
injection into Object.

T.

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 9, 2007, 8:33am UTC](https://www.ruby-forum.com/t/that-top-level-self/119690/3 "2007-11-09T08:33:30Z")

</div>

On Nov 9, 2007 6:06 PM, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The ruby top-level ‘self’ seems like a strange beast. It is an instance  
> of Object. But definitions of methods, modules, classes, and constants  
> magically go into the Object class. (So say the reflection methods.)
> 
> (In “irb” the methods are public. In “ruby” they are private.)
> 
> Can this be understood as less magical?

Maybe. Ruby simply has two separate notions of “current object”  
(self), and “current class” (the thing which receives method  
definitions). This might help:

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/228855](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/228855)

Regards,  
George.
