Last active
August 29, 2015 14:04
-
-
Save domgetter/5ffaee7030af0facb096 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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