Skip to content

Instantly share code, notes, and snippets.

@snuxoll
Created April 20, 2015 20:29
Show Gist options
  • Save snuxoll/e6aade03f293fd5416ae to your computer and use it in GitHub Desktop.
Save snuxoll/e6aade03f293fd5416ae to your computer and use it in GitHub Desktop.
class FoodException < Exception
end
class Food
attr_reader :type
def initialize(type)
@type = type
end
end
class Diet
def initialize(acceptable_foods)
@acceptable_foods = acceptable_foods
end
def ensure_edible(food)
raise FoodException.new unless @acceptable_foods.include? food.type
end
end
class Animal
def initialize(stomach, diet)
@stomach = stomach
@diet = diet
end
def eat(food)
begin
@diet.ensure_edible(food)
@stomach.eat(food)
puts "Yum"
rescue FoodException => e
puts "Yuck"
end
end
end
class Stomach
SMALL = 3
MEDIUM = 5
LARGE = 8
def initialize(size)
@size = size
@food = []
end
def eat(food)
raise FoodException.new if @food.length >= size
@food << food
end
end
end
HerbivoreDiet = Diet.new([:vegtable, :fruit])
small_herbivore = Animal.new(Stomach.new(Stomach::SMALL), HerbivoreDiet))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment