Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > instance method adding another instance method to the class

Reply
Thread Tools

instance method adding another instance method to the class

 
 
Raj Singh
Guest
Posts: n/a
 
      05-29-2008
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Dave Bass
Guest
Posts: n/a
 
      05-29-2008
Raj Singh 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.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Rick DeNatale
Guest
Posts: n/a
 
      05-29-2008
On Thu, May 29, 2008 at 5:31 PM, Dave Bass <(E-Mail Removed)> wrote:
> Raj Singh 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.


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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
injecting an instance method into another class' class method Chuck Remes Ruby 3 08-12-2010 07:15 PM
Don't understand behavior; instance form a class in another class'instance Martin P. Hellwig Python 1 03-26-2010 12:06 AM
Adding method from one class to another class or to instance ofanother class marekw2143 Python 3 07-25-2009 08:33 PM
why can't an instance instantiated within a class method access aprotected instance method? Greg Hauptmann Ruby 9 06-16-2008 10:16 AM
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM



Advertisments