Possible Duplicate:
class << self idiom in Ruby
I have a quick Ruby question. I come from a Java/c background, so I understand in Ruby "self" when referenced inside a instance method acts like "this". And "self." prefix for method defines it as a class method.
But what does this mean here??
class << self
def some_method
end
end
What's happening here is that we're reopening an object's class from within itself and defining a new instance method on it. This is one of the ways you can do so-called "monkey-patching" in Ruby. This method adds the method only to the current object rather than all objects of the class.
It's equivalent to doing this:
my_obj = MyClass.new
class << my_obj
def some_method
end
end
# or...
def my_obj.some_method
end
Here's a good article that covers it: Learning Ruby: class << self.