Skip to content

Instantly share code, notes, and snippets.

@hieuk09
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hieuk09/eec5487a0a6ff7c4d674 to your computer and use it in GitHub Desktop.
Save hieuk09/eec5487a0a6ff7c4d674 to your computer and use it in GitHub Desktop.
Polymorphic sample
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