Skip to content

Instantly share code, notes, and snippets.

@daneharrigan
Created May 21, 2011 16:29
Show Gist options
  • Save daneharrigan/984655 to your computer and use it in GitHub Desktop.
Save daneharrigan/984655 to your computer and use it in GitHub Desktop.
4 ways to define class methods
# most people do
class Foo
def self.method1
puts "method1"
end
end
# or this
class Foo
class << self
def method2
puts "method2"
end
end
end
# this was new to me, but it makes sense
class Foo
def Foo.method3
puts "method3"
end
end
# i don't know if this is supposed to work, but it does. no need to open the class to define something on it
def Foo.method4
puts "method4"
end
# they all work
Foo.method1 # => "method1"
Foo.method2 # => "method2"
Foo.method3 # => "method3"
Foo.method4 # => "method4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment