A lot of answers already, but here is why self is the class:
The dot changes self
to whatever is before the dot. So, when you do foo.bar
then for the bar
-method, self
is foo
. There is no difference with class methods. When calling Post.cool_post
, you'll change self
to Post
.
The important thing to note here is that it's not how the method is defined that determines self
, but how it's called. This is why this works:
class Foo
def self.bar
self
end
end
class Baz < Foo
end
Baz.bar # => Baz
Or this:
module Foo
def bar
self
end
end
class Baz
extend Foo
end
Baz.bar # => Baz