Created
December 3, 2013 02:24
-
-
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.
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 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