Created
August 7, 2012 09:55
-
-
Save smtalim/3284105 to your computer and use it in GitHub Desktop.
Anonymous Class
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
# Here are some ways by which you can define an anonymous class | |
# 1 | |
class Rubyist | |
def self.who | |
"Geek" | |
end | |
end | |
# 2 | |
class Rubyist | |
class << self | |
def who | |
"Geek" | |
end | |
end | |
end | |
# 3 | |
class Rubyist | |
end | |
def Rubyist.who | |
"Geek" | |
end | |
#4 | |
class Rubyist | |
end | |
Rubyist.instance_eval do | |
def who | |
"Geek" | |
end | |
end | |
puts Rubyist.who # => Geek | |
# 5 | |
class << Rubyist | |
def who | |
"Geek" | |
end | |
end | |
# All five snippets above, define a Rubyist.who that returns Geek. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment