Giter Club home page Giter Club logo

pfwtfp-hash-drills-atl-se-010620-gap's Introduction

Hash Drills

Learning Goals

  • Access specific data in Hashes
  • Modifying data in Hashes
  • Perform operations on Hashes guided by tests

Introduction

We've discussed a few different ways that Hashes can be created and how useful they are in storing and retrieving associated data with ease. In this lesson, we're going to practice accessing and modifying Hashes.

Access Specific Data from Hashes

We're going to host a dinner party! We have a list of nutritious ingredients we will be using to make dinner, but we want to prepare separate dishes for guests with dietary restrictions: vegetarian, ketogenic, and mediterranean.

Here is our full list of ingredients:

shopping_list = {
    :sweets => ["soda", "candy", "potato chips"],
    :protein => {
        :meat => ["chicken", "fish", "steak"],
        :other => ["eggs", "nuts","beans"]
    },
    :dairy => ["milk", "yogurt", "cheese"],
    :fruits => ["bananas", "oranges", "apples", "grapes"],
    :vegetables => ["cabbage", "broccoli", "tomatoes", "carrots"],
    :grains => ["crackers", "rice", "bread", "pasta", "cereal"]
}

We will have to make sure only some of these items are used in our special dishes. Since our recipes will require some attention to detail, we're going to write methods that filter out the appropriate content.

We don't want to modify our original list, instead creating new Hashes to represent the 3 separate ingredients lists for our guests with dietary restrictions.

Instructions

In lib/hash_drills.rb, we've got a method, shopping_list, that returns a Hash of our main shopping list. Your first task is to write three methods that return Hashes with only the appropriate keys from our shopping_list Hash:

vegetarian_ingredients: Vegetarian dishes cannot include meat or fish. While some vegetarians are okay with eggs, leave them off as well.

ketogenic_ingredients: Ketogenic dishes have very few carbs and sugars, and cannot include grains, fruits, or sweets. Cheese is great for keto, but not milk or yogurt. Also, leave out the beans.

mediterranean_ingredients: For mediterranean dishes, we can only include chicken, fish, nuts, yogurt, cheese, fruits, vegetables, and grains.

After writing each method, run learn to confirm your solutions before continuing.

Late Additions to the Party

Vegan Guest

It turns out you've got a last minute addition to your guest list: your vegan friend is coming as well! We'll need to prepare a vegan_ingredients list. However, vegan diets are similar to vegetarian diets, so rather than write a method that filters from from our shopping_list, let's use use the Hash returned by vegetarian_ingredients, remove all dairy items from it, and return this updated Hash.

Allergens

You found a great recipe online for sweet and salty party nuts, and are eager to try it out on your guests. However, a dear friend RSVP'd saying she may or may not be able to make it. She has a severe nut allergy, and if she is able to come, you will need to modify all of your ingredients lists to make sure there are no nuts in anything.

Write a method remove_allergens that takes in any one of our ingredients Hashes as an argument, and returns a new Hash with nuts removed.

Conclusion

There are so many things to do to get ready for our party. With a little help from Ruby Hashes, though, we've got our lists of ingredients and can start preparing our menu! The guests will be elated. Bon appetit!

Resources

pfwtfp-hash-drills-atl-se-010620-gap's People

Contributors

maxwellbenton avatar drakeltheryuujin avatar rhodunda avatar sgharms avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.