Git Product home page Git Product logo

programming-univbasics-4-building-nested-hashes-nyc01-seng-ft-062220's Introduction

Building Nested Hashes Lab

Learning Goals

  • Build a nested hash
  • Add key/value pairs in a nested hash
  • Update key/value pairs in a nested hash

Introduction

In this lab, we will practice constructing deeply-nested Hash. This means that we'll have a Hash that contains a Hash inside of it. Those "interior" or "inner" Hashes might themselves contain Hashes.

As a reminder, this might look like:

# Greatly abbreviated!
countries_and_capitals_of_the_world = {
  :north_america => {
    "Canada" => {
      :capital => "Ottawa",
      :capital_climate => "Kőppen Dfb"
    },
    "USA" => {
      :capital => "Washington D.C.",
      :capital_climate => "Kőppen Cfa"
    }
  },
  :africa => {
    "Ghana" => {
      :capital => "Accra",
      :capital_climate => "Kőppen Aw"
    },
    "Nigeria" => {
      :capital => "Abuja",
      :capital_climate => "Kőppen Aw"
    }
  }
}

We will work through this lab by filling out the implementation of four methods. Each method will return a Hash that is locally defined within that method's implementation. The comments in ./intro_to_ruby_hashes_lab.rb and the results of running the tests will guide you.

Process

We're going to write the implementation of 4 methods to gradually build out a deeply nested hash:

  • base_hash
  • monopoly_with_second_tier
  • monopoly_with_third_tier
  • monopoly_with_fourth_tier

For the second, third and fourth methods, we'll be updating the hash we created in the previous method, only one tier deeper into the hash.

This means that after successfully implementing the first method, bash_hash, copy your solution into monopoly_with_second_tier. There are some additional expectations for the monopoly_with_second_tier, so what you just copied won't quite work. Modify the hash until you're passing all the tests for this second method. Repeate this process by copying this solution into monopoly_with_third_tier.

This lab has a detailed set of tests to guide you, so use learn to get additional information on what is required.

bash_hash

This method should do the following:

  • return a hash with one key called :railroads that points to an empty hash

monopoly_with_second_tier

This method builds on the hash returned by base_hash, updating the nested :railroads hash so that it includes

  • a key called :pieces set to 4
  • a key called :rent_in_dollars set to an empty hash
  • a key called :names set to an empty hash

monopoly_with_third_tier

This method builds on the hash returned by monopoly_with_second_tier updating the nested :rent_in_dollars hash so that it includes:

  • a :one_piece_owned key set to 25
  • a :two_pieces_owned key set to 50
  • a :three_pieces_owned key set to 100
  • a :four_pieces_owned key set to 200

In addition, this method should also update the nested :names hash so that it includes:

  • a :reading_railroad key that points to an empty hash
  • a :pennsylvania_railroad key that points to an empty hash
  • a :b_and_o_railroad key that points to an empty hash
  • a :shortline_railroad key that points to an empty hash

monopoly_with_fourth_tier

This method builds on the hash returned by monopoly_with_third_tier, updating each of the nested hashes inside the :names hash so that

  • the hash assigned to :reading_railroad includes a :mortgage_value key set to 100
  • the hash assigned to :pennsylvania_railroad includes a :mortgage_value key set to 200
  • the hash assigned to :b_and_o_railroad includes a :mortgage_value key set to 400
  • the hash assigned to :shortline_railroad includes a :mortgage_value key set to 800

If you get stumped, we've included the final Hash in the file final_hash.rb in this repository.

PATTERN FOR LEARNING: We've seen many students take the final answer and pare it down to get the tests passing. This is not a good path to mastering this concept. The path that requires work in programming, at the gym, or in relationships creates the most benefit. Passing tests now is not as valuable as understanding these concepts later.

Conclusion

Congratulations! You've built a pretty complex Hash with nesting. This should prove to you that there's very little in this world that can't be modeled with nesting of Hashes, scalar values, and Arrays.

Resources

programming-univbasics-4-building-nested-hashes-nyc01-seng-ft-062220's People

Contributors

maxwellbenton avatar sophiedebenedetto avatar msuzoagu avatar deniznida avatar victhevenot avatar markedwardmurray avatar jonathanx111 avatar ga-be avatar ehawk823 avatar fislabstest avatar octosteve avatar sarogers avatar roseweixel avatar pletcher avatar fs-lms-test-bot avatar aviflombaum avatar ahimmelstoss avatar

Watchers

 avatar Mohawk Greene avatar  avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar  avatar  avatar  avatar  avatar Matt avatar Antoin avatar  avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman avatar Lisa Jiang avatar Vicki Aubin avatar  avatar  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.