# Instance method adding another instance method to the class

**URL:** <https://www.ruby-forum.com/t/instance-method-adding-another-instance-method-to-the-class/139003>\
**Category:** Ruby\
**Created:** [May 29, 2008, 9:25pm UTC](https://www.ruby-forum.com/t/instance-method-adding-another-instance-method-to-the-class/139003 "2008-05-29T21:25:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 29, 2008, 9:25pm UTC](https://www.ruby-forum.com/t/instance-method-adding-another-instance-method-to-the-class/139003/1 "2008-05-29T21:25:56Z")

</div>

class Person  
end

p = Person.new

def p.something  
def speaks  
puts ‘i speak’  
end  
end

p.something  
Person.new.speaks

I am trying to understand why the last line is working.

When p.something is executed then the self is ‘p object’. Now method  
speaks is being defined while the self is p. Since speaks is an instance  
method a method should be defined in p’s metaclass. But p’s meta class  
is not Person.

Any one cares to explain how method speaks get defined as an instance  
method for Person class.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 29, 2008, 11:31pm UTC](https://www.ruby-forum.com/t/instance-method-adding-another-instance-method-to-the-class/139003/2 "2008-05-29T23:31:50Z")

</div>

Raj S. wrote:

> Any one cares to explain how method speaks get defined as an instance  
> method for Person class.

It’s not necessary to instantiate Person; changing the last line to  
Person.speaks also works. This indicates that speak is being defined as  
a class method. This can be confirmed if you put a “p self” line in:

class Person  
end

p = Person.new

def p.something  
def speaks  
puts ‘i speak’  
p self  
end  
end

p.something  
Person.speaks

which gives:

i speak  
Person

How it works is beyond me though.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 30, 2008, 12:10am UTC](https://www.ruby-forum.com/t/instance-method-adding-another-instance-method-to-the-class/139003/3 "2008-05-30T00:10:25Z")

</div>

On Thu, May 29, 2008 at 5:31 PM, Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Person.speaks
> 
> which gives:
> 
> i speak  
> Person
> 
> How it works is beyond me though.

It’s because a nested def doesn’t define a method in the class of the  
receiver of the outer method. So in this case it’s just as if the code  
was written simply:

def speaks  
puts "I speak  
end

except that this doesn’t get evaluated until the something method is  
run.

So it ends up defining this method, as all top-level methods are, in  
the Kernel module, so any object will respond to speaks.

To get what I think the OP intended, which is to define speaks as a  
singleton method when the singleton method something is run, try

def p.something  
def self.speaks  
puts “I speak”  
end  
end

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
