Created
July 26, 2010 07:29
-
-
Save ugisozols/490280 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 bark | |
"#{@name} barks like a dog" | |
end | |
def eat | |
"#{@name} eats like a dog" | |
end | |
def chase_cat | |
"#{@name} chases cats like a dog" | |
end | |
def teach_trick(trick, &block) | |
instance_eval %{ | |
def #{trick} | |
"#{instance_eval &block}" | |
end | |
} | |
end | |
def method_missing(method) | |
"#{@name} doesn't know how to #{method}!" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
d = Dog.new('Fido')
puts d.bark
puts d.chase_cat
d.teach_trick('fly') {"#{@name} Flies like a bird"}
puts d.fly
puts Dog.new('Barky').fly