Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created November 4, 2022 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/8a221da686c9540af1d1783181c9c9a6 to your computer and use it in GitHub Desktop.
Save JoshCheek/8a221da686c9540af1d1783181c9c9a6 to your computer and use it in GitHub Desktop.
What even is a "class method"?
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