small medium large xlarge

25 May 2009, 07:47
Jon Stenqvist (5 posts)

I just seen your excellent presentation from SOR. This was a good repetition of the ruby object model after seen the ruby meta programming seriers.

Could someone help me understand the following, that is, in side a class definetion, when i puts self. i get the current class.

class User
  # Explicit set the receiver, the methods is defined the ghost-class
  def self.find

  # This method will be defined in the User class, becouse self is not set explicit, why does this method end up in User class and not the ghost-class? 
  # Inside a method self.call_some_method and call_some_method the samething, why is it diffrent when a method is defined?
  def name
25 May 2009, 14:33
Dave Thomas (352 posts)

Ruby sets the value of self inside class and module definitions.

When you define def self.find, you’re not setting the receiver—thats what happens on method call. Instead, you’re forcing Ruby to add the method to the ghost class. Then, on the call to User.find, the receiver gets set to User, and the method gets looked for (and found) in User’s class (which is the ghost class).


You must be logged in to comment