Last active
August 29, 2015 14:07
-
-
Save hieuk09/eec5487a0a6ff7c4d674 to your computer and use it in GitHub Desktop.
Polymorphic sample
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 | |
def name | |
'animal' | |
end | |
end | |
class Cow < Animal | |
def name | |
'cow' | |
end | |
end | |
class Chicken < Animal | |
def name | |
'chicken' | |
end | |
end | |
class Farm | |
attr_accessor :animals | |
def initalize(animals) | |
@animals = animals | |
end | |
def animal_names | |
animals.map(&:name) | |
end | |
def self.animal_names | |
animals = [Cow, Chicken].map(&:new) | |
new(animals).animal_names | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment