Skip to content

Instantly share code, notes, and snippets.

@juev
Forked from smtalim/anonymous_class.rb
Created August 8, 2012 09:36
Show Gist options
  • Save juev/3293785 to your computer and use it in GitHub Desktop.
Save juev/3293785 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