This Ruby Style Guide tells that is better using self.method_name
instead of class method_name
. But Why?
class TestClass
# bad
So far the question and answers only discuss these two options:
class MyClass
def self.method_name
..
end
end
class MyClass
class << self
def method_name
..
end
end
end
There's a third option to consider for class methods:
class MyClass
def MyClass.method_name
..
end
end
It's not popular and is more verbose, but it's the most explicit option.
It's also less confusing if you mix up self
behaviour between Python and Ruby.