# Why?

**URL:** <https://www.ruby-forum.com/t/why/151506>\
**Category:** Ruby\
**Created:** [October 22, 2008, 8:52am UTC](https://www.ruby-forum.com/t/why/151506 "2008-10-22T08:52:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyung\_won\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/47e85d/32.png) [@Kyung\_won\_C](https://www.ruby-forum.com/u/Kyung_won_C)\
**Post date:** [October 22, 2008, 8:52am UTC](https://www.ruby-forum.com/t/why/151506/1 "2008-10-22T08:52:03Z")

</div>

## in irb

> > self  
> > =\> main
> 
> > def why?  
> > end  
> > =\> nil
> 
> > self.why?  
> > =\> nil
> 
> >

## in test.rb

p self # =\> main  
def why?  
end  
self.why? # private method ‘why?’ called for main:Object  
(NoMethodError)

## Anyway, one more…

## in test2.rb

def why?  
end

class A  
end

p A.private\_method\_defined?(:why?) # true

# How is ‘why?’ inherited???

# The ‘main’ is a instance of Object but it’s not a Object?

# I understand if …

# class Object

# private

# def why?

# end

# end

##############

# Help Me^^

##############

---

<div class="post-metadata">

**Author:** ![Kyung\_won\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/47e85d/32.png) [@Kyung\_won\_C](https://www.ruby-forum.com/u/Kyung_won_C)\
**Post date:** [October 22, 2008, 1:10pm UTC](https://www.ruby-forum.com/t/why/151506/2 "2008-10-22T13:10:05Z")

</div>

On Oct 22, 2:50 am, Kyung won Cheon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> nil
> 
> # How is ‘why?’ inherited???
> 
> # Help Me^^
> 
> ##############

The toplevel object (aka ‘main’) delegates some module-equivalent  
methods to Object class. So when you say

def why?  
end

What actually happens is:

class Object  
def why?  
end  
private :why?  
end

Not all module methods are delegated, try using define\_method(:why?)  
at the toplevel instead and it will bomb.

T.

P.S. Personally, I find the whole setup rather half-baked, and have  
continually advocated for the replacement of the current toplevel  
object with a self extended module.

---

<div class="post-metadata">

**Author:** ![Kyung\_won\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/47e85d/32.png) [@Kyung\_won\_C](https://www.ruby-forum.com/u/Kyung_won_C)\
**Post date:** [October 22, 2008, 1:28pm UTC](https://www.ruby-forum.com/t/why/151506/3 "2008-10-22T13:28:52Z")

</div>

Trans wrote:

> The toplevel object (aka ‘main’) delegates some module-equivalent  
> methods to Object class.

As far as I’m aware it “delegates” only alias, undef, def, module and  
class.  
So it only delegates keywords, not methods. Which is, I assume, why def  
works  
and define\_method (which is a method) does not.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![Kyung\_won\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/47e85d/32.png) [@Kyung\_won\_C](https://www.ruby-forum.com/u/Kyung_won_C)\
**Post date:** [October 22, 2008, 9:08pm UTC](https://www.ruby-forum.com/t/why/151506/4 "2008-10-22T21:08:46Z")

</div>

On Oct 22, 7:25 am, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Trans wrote:
> 
> > The toplevel object (aka ‘main’) delegates some module-equivalent  
> > methods to Object class.
> 
> As far as I’m aware it “delegates” only alias, undef, def, module and class.  
> So it only delegates keywords, not methods. Which is, I assume, why def works  
> and define\_method (which is a method) does not.

Good point. I never really looked at like that b/c I tend to think of  
keywords as syntax sugar for real methods.

class → Class.new  
def → define\_method  
alias → alias\_method  
etc.

Conditionals are an exception, of course.

But it sort of begs the question, why does it support the one and not  
the other?

T.
