Skip to content

Instantly share code, notes, and snippets.

@tonini
Created November 20, 2009 20:27
Show Gist options
  • Save tonini/239749 to your computer and use it in GitHub Desktop.
Save tonini/239749 to your computer and use it in GitHub Desktop.
# singleton_class.rb
# zastav [@samueltonini]
obj = Object.new
def obj.who_i_am?
puts "I'm a singleton method"
end
singleton_class = class << obj
def and_who_i_am?
puts "I'm a singleton method defined in a singleton class or also called eigenclass"
end
def bye
puts "bye dude!"
end
self # singleton class object
end
obj.who_i_am? # I'm a singleton method
obj.and_who_i_am? # I'm a singleton method defined in a singleton class or also called eigenclass
obj.bye # bye dude!
p singleton_class.instance_methods(false) # [:who_i_am?, :and_who_i_am?, :bye]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment