Skip to content

Instantly share code, notes, and snippets.

@kblake
Created April 8, 2011 20:40
Show Gist options
  • Save kblake/910684 to your computer and use it in GitHub Desktop.
Save kblake/910684 to your computer and use it in GitHub Desktop.
# 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