Skip to content

Instantly share code, notes, and snippets.

@ivanbrennan
Created September 30, 2013 22:11
Show Gist options
  • Select an option

  • Save ivanbrennan/6771070 to your computer and use it in GitHub Desktop.

Select an option

Save ivanbrennan/6771070 to your computer and use it in GitHub Desktop.
Holiday Supplies
holiday_supplies = {
:winter => {
:christmas => ["Lights", "Wreath"],
:new_years => ["Party Hats"]
},
:summer => {
:forth_of_july => ["Fireworks", "BBQ"]
},
:fall => {
:thanksgiving => ["Turkey"]
},
:spring => {
:memorial_day => ["BBQ"]
}
}
# 1. How would you access the second supply for the forth_of_july? ex: holiday_supplies[:spring][:memorial_day][0]
holiday_supplies[:summer][:forth_of_july][1]
# 2. Add a supply to a Winter holiday.
holiday_supplies[:winter][:new_years] << ["Alibie"]
# 3. Add a supply to memorial day.
holiday_supplies[:spring][:memorial_day] << ["Potato salad"]
# 4. Add a new holiday to any season with supplies.
holiday_supplies[:fall][:haloween] = ["Costume", "Candy", "Pumpkins"]
# 5. Write a method to collect all Winter supplies from all the winter holidays. ex: winter_suppliers(holiday_supplies) #=> ["Lights", "Wreath", etc]
def winter_suppliers(holiday_supplies)
supplies = []
winter_holidays = holiday_supplies[:winter].keys
winter_holidays.each do |holiday|
these_supps = holiday_supplies[:winter][holiday]
these_supps.each { |supply| supplies << supply }
end
supplies
end
def better_winter_suppliers(holiday_supplies)
holiday_supplies[:winter].values.flatten
end
# 6. Write a loop to list out all the supplies you have for each holiday and the season.
holiday_supplies.each do |season, season_holidays|
puts "#{season.capitalize}:"
season_holidays.each do |holiday, supplies|
all_supplies = supplies.join(" and ")
str_holiday = holiday.to_s.capitalize.gsub("_"," ")
puts " #{str_holiday}: #{all_supplies}"
end
end
# 7. Write a method to collect all holidays with BBQ.
def bbq_holidays(holiday_supplies)
bbqs = []
seasons = holiday_supplies.values
seasons.each do |season|
season.each do |holiday, supplies|
bbqs << holiday if supplies.include?("BBQ")
end
end
bbqs
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment