Understanding class methods in ruby

by Frank

First of all, let’s go back to the basics. What we generally call a class method is a method that resides at the class level. On the opposite, an instance method is a method that resides at the object level.

#UPDATE 2007/04/15
I changed the previous paragraph and removed the word ‘shared’ when talking about class methods because it was confusing. A class method isn’t ‘shared’ since it’s instances cannot access it without using explicitly the reference to the class, like that : my_obj.class.class_method. And no, unlike someone have suggested, the super keyword won’t work. I’ve just been too quick to update my post… sorry)
END UPDATE

In ruby, classes are also objects, so the methods you define as class methods only exist in the object that defined them (the class) and nowhere else. If you define my_class_method in a class named Test, my_class_method will only live into the object Test, which is an instance of a built-in ruby class named Class. If what I just said sounded like mandarin, you might want to read this post first.

Every instances that you create contain a variable that points to their corresponding class. Say you create a new instance of class Test and that you name this instance x (x = Test.new). x will contain every instance variables/methods that have been defined as such in the class as well as a reference to the object Test, which will contain the variables that need to be shared among every instances of this class (class variables) as well as methods that will be local to this object (class methods). That’s the reason why ruby complain when you write something like x.my_class_method. It complains because my_class_method just doesn’t reside in x, it resides in Test.

You have 2 ways for defining a class method.

1) You can use the name of the class directly

class Test #Test is now an instance of a built-in class named Class
  def Test.class_method
    "I'm a class method."
  end
end

2) You can use the self variable, which is always pointing to the current object

class Test
  def self.class_method
    "I'm a class method."
  end
end

Once you understand that classes are objects, this use of the self variable to define a class method finally makes sense.

In fact, there is another way to define a class method. I personally find it a little bit more obscure. it consists of building a new anonymous class only for the current object (self). To create the anonymous class, we use the << notation.

class Test
  def some_method
    "bla bla bla"
  end

  class <<self
    def my_class_method1
        "Weirdo"
    end

    def my_class_method2
        "But use it if you want"
    end
  end
end

Remember what we have to do to add a new instance method in a class :

class Test
  def hello
    "Hello! I'm an instance method"
    "I'm accessible in every single instance of class Test"
  end
end

x = Test.new
#Add a new method to an instance of class Test
def x.goodbye
  "Goodbye! I'm an instance method"
  "I only exists in x"
end

When you look at it, it's exactly the same trick that we previously used to add a new class method. The only difference is that instead of adding a method to an instance of class Class, we added a method to an instance of class Test.

The value of self

Not too surprinsingly, when you are inside a class method, the value of self refers to the object that holds the class structure (the instance of class Class). This means that :

class Test
  def self.class_method
    self.x
  end
end

is equivalent to :

class Test
  def self.class_method
    Test.x
  end
end

When you are inside an instance method, the value of self still refers to the current object. This time however, the current object is an instance of class Test, not an instance of class Class.

#UPDATE (10/31/2007)
The rest of this post was not relevant, a bit confusing and just not accurate enough for my taste. Thus, it has been removed!

Bookmark this post : These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Reddit
  • del.icio.us
  • Digg
  • Furl
  • Technorati
  • StumbleUpon