Skip to content

Instantly share code, notes, and snippets.


amy-mac/Gluten_Free.rb Secret

Created Sep 29, 2013
What would you like to do?
Gluten Free lab project for WDI - Week One
#Title: Gluten Free
#- Ruby
# - Exceptions
# - Hashes & Arrays
# - Objects & Classes
#- Work with exceptions, classes, class variables, conditions
# ======================================================================
# Create a Person class. A person will have a stomach and allergies
# Create a method that allows the person to eat and add arrays of food to their stomachs
# If a food array contains a known allergy reject the food.
class AllergyError < StandardError
class Person
attr_accessor :stomach, :allergies
def initialize(allergies)
@allergies = allergies
@stomach = []
def allergy_test
@stomach.each do |food|
if food == @allergies
@stomach = []
raise AllergyError
def eat(foods)
@stomach << foods
puts "You now have #{@stomach.join(', ')} in your stomach."
rescue AllergyError
puts "Uh oh, you've eaten #{@allergies} and threw up."
# Create a Person named Chris. Chris is allergic to gluten.
# Create a Person named Beth. Beth is allergic to scallops.
# Feed them the following foods
pizza = ["cheese", "gluten", "tomatoes"]
pan_seared_scallops = ["scallops", "lemons", "pasta", "olive oil"]
water = ["h", "h", "o"]
chris ='gluten')
beth ='scallops')
# When a person attempts to eat a food they are allergic to, raise a custom exception
# For example: AllergyError
# Bonus: When a person attempts to eat a food they are allergic to,
# ... remove ALL the food from the person's stomach before raising the exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment