The syntax class << some_objct opens the class some_object singleton, which is a special "secret" class to which only this object belongs. Using the singleton class, you can define the methods that one object responds to, while other instances of its normal class do not.
So for example:
a = "Hello world" b = "Hello world"
In the context of a class, these two method definitions are equivalent:
class Foo def self.bar puts "Yo dawg" end end class Foo class << self def bar puts "Yo dawg" end end end
The second form may be useful in certain circumstances (for example, when you want to declare attr_accessor for the class object itself).
Chuck source share