Created
October 28, 2015 22:50
-
-
Save kkchu791/7279a1e548b6385d8621 to your computer and use it in GitHub Desktop.
This file contains 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 Dog | |
def initialize name | |
@name = name | |
end | |
def teach_trick(trick, &code) | |
define_singleton_method(trick, &code) | |
end | |
def method_missing(trick) | |
puts "#{@name} doesn't know how to #{trick}" | |
end | |
end | |
d = Dog.new('Lassie') | |
d.teach_trick(:dance) { "#{@name} is dancing!" } | |
puts d.dance | |
d.teach_trick(:poo) { "#{@name} is a smelly doggy!" } | |
puts d.poo | |
d2 = Dog.new('Fido') | |
puts d2.dance | |
d2.teach_trick(:laugh) { "#{@name} finds this hilarious!" } | |
puts d2.laugh | |
puts d.laugh | |
d3 = Dog.new('Stimpy') | |
puts d3.dance | |
puts d3.laugh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment