Skip to content

Instantly share code, notes, and snippets.

@ugisozols
Created July 26, 2010 07:29
Show Gist options
  • Save ugisozols/490280 to your computer and use it in GitHub Desktop.
Save ugisozols/490280 to your computer and use it in GitHub Desktop.
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
@kotp
Copy link

kotp commented Jul 26, 2010

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment