Created
April 8, 2011 20:40
-
-
Save kblake/910684 to your computer and use it in GitHub Desktop.
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
# The goal is take type specific conditionals out of a class and use polymorphism to let each class handle their own responsibilities | |
class Animal | |
def initialize(type) | |
@type = type | |
end | |
def eat | |
"eating... nom nom" | |
end | |
def say | |
case @type | |
when :lion | |
"roar" | |
when :mouse | |
"squeak" | |
when :dog | |
"bark" | |
end | |
end | |
end | |
zoo = [Animal.new(:lion), Animal.new(:mouse), Animal.new(:dog)] | |
zoo.each do |animal| | |
puts animal.say | |
puts animal.eat | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment