Created
August 27, 2013 20:19
-
-
Save dmehrotra/6358617 to your computer and use it in GitHub Desktop.
inheritance
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 Animal | |
attr_reader :name | |
def initialize(name) | |
@name = name | |
end | |
def emote | |
[ | |
'make various noises' | |
] | |
end | |
def eat | |
[ | |
'food' | |
] | |
end | |
end | |
class Duck < Animal | |
def emote | |
super + [ | |
"Quack" | |
] | |
end | |
def eat | |
super + [ | |
"snails" | |
] | |
end | |
end | |
class Cat < Animal | |
def emote | |
super + [ | |
"meow", 'purr' | |
] | |
end | |
def eat | |
super + [ | |
"throw up", "kibble" | |
] | |
end | |
end | |
class Dog < Animal | |
def emote | |
super + [ | |
"woof", 'whine' | |
] | |
end | |
def eat | |
super + [ | |
"throw up", "dog food" | |
] | |
end | |
end | |
puts Animal.new('tiger').emote | |
puts Cat.new('Frank').emote | |
puts Dog.new('Jeffery Dogger').eat | |
puts Duck.new('John Duckstein').emote | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment