Skip to content

Instantly share code, notes, and snippets.

@domgetter
Last active August 29, 2015 14:04
Show Gist options
  • Save domgetter/5ffaee7030af0facb096 to your computer and use it in GitHub Desktop.
Save domgetter/5ffaee7030af0facb096 to your computer and use it in GitHub Desktop.
class Object
def eigen
str = caller[0]
/\w+(?=')/ =~ str
singleton_class.send $&.to_sym
end
end
class Animal
def speak
puts "An animal made noise"
end
end
class Dog < Animal
def speak
puts "A dog spoke"
eigen # calls speak in dog's singleton class's singleton class
super # calls speak in Dog's superclass
end
class << self
def speak
puts "neat"
end
end
end
dog = Dog.new
class << dog # open up dog's singleton class
def speak
puts "#{self}: bowwow"
super # calls speak in Dog class, the super of dog's singleton class
end
class << self # open up dog's singleton class's singleton class
def speak
puts "what?"
super # calls speak in Dog's singleton class, the superclass of dog's singleton class's singleton class
end
end
end
dog.speak
# #<Dog:0x012b532>: bowwow
# A dog spoke
# what?
# neat
# An animal made noise
# => nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment