Skip to content

psswf1172/apples-and-holidays-001-prework-web

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Iterating Over Hashes

Objectives

  1. Iterate over nested, or multidimensional, hashes.

The Holiday Suppliers

Instructions

You have a bunch of decorations for various holidays organized by season.

holiday_supplies = {
  :winter => {
    :christmas => ["Lights", "Wreath"],
    :new_years => ["Party Hats"]
  },
  :summer => {
    :fourth_of_july => ["Fireworks", "BBQ"]
  },
  :fall => {
    :thanksgiving => ["Turkey"]
  },
  :spring => {
    :memorial_day => ["BBQ"]
  }
}

Write your methods in lib/holiday.rb; use the comments in each method as guides.

  • Write a method that returns the second supply for the Fourth of July. For eg:
def second_supply_for_fourth_of_july(holiday_supplies)
  holiday_supplies[:summer][:fourth_of_july][1]
end
  • Write a method that adds a supply to both Winter holidays.

  • Write a method that adds a supply to Memorial Day.

  • Write a method that adds a new holiday and its associated supplies to any season.

  • Write a method to collect all Winter supplies from all the winter holidays. For eg:

winter_supplies(holiday_supplies) #=> ["Lights", "Wreath", etc]
  • Write a method that uses a loop to list out all the supplies you have for each holiday and the season. Use string manipulation to get your output to match what the test is expecting.

  • Here are a few helpful tips:

    • Our hash keys are symbols. We need to convert them into strings. Use the .to_s method on a symbol to convert it into a string.
    • Look closely at the output string that the test is expecting. You'll notice that it expects holiday names, like "New Years", to have both words capitalized. Ruby has a .capitalize method that you can call on a string. But, note:
      • .capitilize returns the capitalized string but doesn't change the original string. So, when you call on that same string in the future, it isn't capitalized!. You can capitalize a string for now and evermore by using the !, bang operator.
      • You'll need to capitalize both words in a given holiday's name. If you call "new years".capitalize!. It will return "New years". In order to capitalize both words, you'll need to .split the string into an array and iterate over that array to .capitalize! each word in it. Then, you'll need to .join the array back into a string.
      • If you're unfamiliar with the methods mentioned above, look them up in the Ruby documentation.

Example of expected output:

Winter:
  Christmas: Lights, Wreath
  New Years: Party Hats
  • Write a method to collect all holidays with BBQ. The method should behave as seen below:
holidays_with_bbqs(holiday_supplies)
#=> [:fourth_of_july, :memorial_day]

Reminder: This is a challenging lab, so remember to use Pry, googling and the Learn community to help you get the tests passing.

Resources

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%