Skip to content

Instantly share code, notes, and snippets.

@smtalim
Created August 7, 2012 09:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smtalim/3284105 to your computer and use it in GitHub Desktop.
Save smtalim/3284105 to your computer and use it in GitHub Desktop.
Anonymous Class
# 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