Skip to content

Instantly share code, notes, and snippets.

@sjwats
Created December 3, 2013 02:24
Show Gist options
  • Select an option

  • Save sjwats/7762850 to your computer and use it in GitHub Desktop.

Select an option

Save sjwats/7762850 to your computer and use it in GitHub Desktop.
OOPII - Inheritance - Quick Challenge - write a set of classes that represent a set of animals. There should be a Duck, Cat, and Dog instance that all implement an emote (**hint**: what sound does a duck make?) and a eat method. A constructor that takes a name as an argument should be defined elsewhere.
class Animal
attr_reader :name
def initialize(name)
@name = name
end
def eat
["Stuff in nature"]
end
end
class Cat < Animal
def emote
"Meow"
end
def eat
["Cat food"]
end
end
class Dog < Animal
def emote
"Woof"
end
def eat
["Anything"]
end
end
class Duck < Animal
def emote
"Quack"
end
def eat
super + [ "Duck treats" ]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment