Created
May 21, 2011 16:29
-
-
Save daneharrigan/984655 to your computer and use it in GitHub Desktop.
4 ways to define class methods
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
# 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