I've just completed Ruby koan 248 and toward the end of the about_class_methods,rb file there's this section of code:
class Dog
def self.class_method2
:another_way_to_write_class_methods
end
end
def test_you_can_use_self_instead_of_an_explicit_reference_to_dog
assert_equal :another_way_to_write_class_methods, Dog.class_method2
end
# ------------------------------------------------------------------
class Dog
class << self
def another_class_method
:still_another_way
end
end
end
def test_heres_still_another_way_to_write_class_methods
assert_equal :still_another_way, Dog.another_class_method
end
# THINK ABOUT IT:
#
# The two major ways to write class methods are:
# class Demo
# def self.method
# end
#
# class << self
# def class_methods
# end
# end
# end
#
# Which do you prefer and why?
# Are there times you might prefer one over the other?
I'm not sure I appreciate why I might, in different circumstances, prefer one syntax over the other. I can see that you might be able to define a set of methods in the class << self section, but is THAT the point being made? I assume there's something subtle I'm missing, but I don't know what that is.
Would anyone mind spelling it out to me?