We prefer using self.method_name
instead of the class << self
way
of defining class methods.
class Apple
def is_green?
end
class << self
def all_apples
end
end
def another_method
end
end
When reading this class with the number of methods defined,
it's hard to tell the difference between a class method and an instance method
when the methods are using class << self
.
class Apple
def is_green?
end
def self.all_apples
end
def another_method
end
end
Here, the class method is clearly defined and there is no ambiguity when reading this class. Moreover, it is now easier to refactor and maintain.