What even is a "class method"?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
def self.b # we all agree this is a class method | |
'A.b' | |
end | |
end | |
A.b # => "A.b" | |
class << A | |
def c # but what about this? it's the same thing, but we define it differently | |
'A.c' | |
end | |
end | |
A.c # => "A.c" | |
def A.d # Same thing here, this is equivalent to A.b and A.c | |
'A.d' | |
end | |
A.d # => "A.d" | |
class Class | |
def e # and what about this? it's not a singleton method, but you call it the same way | |
'A.e' | |
end | |
end | |
A.e # => "A.e" | |
A = Object.new | |
A.instance_eval do | |
def self.f # and what about this? we define it the same as A.b, but self isn't a Class | |
"A.f" | |
end | |
end | |
A.f # => "A.f" | |
A # => #<Object:0x0000000152061618> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment