Skip to content

Instantly share code, notes, and snippets.

@kkchu791
Created October 28, 2015 22:50
Show Gist options
  • Save kkchu791/7279a1e548b6385d8621 to your computer and use it in GitHub Desktop.
Save kkchu791/7279a1e548b6385d8621 to your computer and use it in GitHub Desktop.
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