Giter VIP home page Giter VIP logo

simple-nesting's Introduction

Simple Nesting Lab

Objectives

  • Access data from a nested hash
  • Add data to and change data within a nested hash

Introduction

Now that we've seen some examples of nested hashes, the next step is to get more comfortable with using them. In this lesson, we're going to practice accessing and updating data within a nested hash.

Instructions

You'll be filling out the content of a series of methods that we've defined for you. All of the methods expect you to operate on the programmer_hash object, which we've already defined and included in the body of each method. Use the test suite to guide you. Each test is designed to tell you how to pass it.

Keep in mind that you are expected to get these tests to pass by adding or changing information in the hash programmatically. In other words, if you're asked to, for example, change the value of a certain hash key, don't just re-write the hash with the new value! Use the methods we've learned and practiced in previous lessons.

To quickly review, here is an example of adding data to a hash programmatically:

# good example

my_hash = {first: "i'm first!", second: "i'm second!"}
my_hash[:third] = "i'm third!"

puts my_hash
# > {first: "i'm first!", second: "i'm second!", third: "i'm third!"}

Changing a hash by simply re-defining it is not the goal of this lab:

# bad example

my_hash = {first: "i'm first!", second: "i'm second!"}
my_hash = {first: "i'm first!", second: "i'm second!", third: "i'm third!"}

Hint: In some of these methods, you will need to alter programmer_hash, then make sure to return the entire hash, not just the altered value.

Conclusion

Adding and updating data in a nested hash is just like adding and updating a normal hash. If we know the structure, it is just a matter of using the right combination of keys.

As we see examples of more complex data structures, we'll start to encounter situations where we aren't 100% of the structure of a hash. This is one reason why getting familiar with programmatically changing hash data is critical - we won't always be able to see the hash we're changing. Instead, we'll have to write logic that and correctly handles accessing and updating hash data in a more abstracted way.

simple-nesting's People

Contributors

aviflombaum avatar benjagross avatar changamanda avatar corasan avatar deniznida avatar fislabstest avatar fs-lms-test-bot avatar ga-be avatar gj avatar ihollander avatar kjleitz avatar kthffmn avatar markedwardmurray avatar maxwellbenton avatar octosteve avatar sarogers avatar sophiedebenedetto avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

simple-nesting's Issues

rspec output during adding_matz

Below is my terminal copy/pasted.
Inside the first error reported it says "expected [:known_for, :languages] to include :yukihiro_matsumoto" which is not correct. It should be expecting adding_matz to include :yukihiro_matsumoto" like it says in the Failure/Error line.

[// โ™ฅ learn --fail-fast
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}

nested hash
#hopper
operates on the programmer_hash and returns the value of the :grace_hopper key
#alan_kay_is_known_for
operates on the programmer_hash and returns the value of what Alan Kay is known for
#dennis_ritchies_language
operates on the programmer_hash and returns the value of Dennis Ritchie's language as a string
#adding_matz
operates on the programmer_hash and adds a key/value pair to the top level of the hash, returning the newly-added-to hash (FAILED - 1)

Failures:

  1. nested hash #adding_matz operates on the programmer_hash and adds a key/value pair to the top level of the hash, returning the newly-added-to hash
    Failure/Error: expect(adding_matz.keys).to include(:yukihiro_matsumoto)
   expected [:known_for, :languages] to include :yukihiro_matsumoto
   Diff:
   @@ -1,2 +1,2 @@
   -[:yukihiro_matsumoto]
   +[:known_for, :languages]

 # ./spec/nested_spec.rb:26:in `block (3 levels) in <top (required)>'

Deprecation Warnings:

RSpec::Core::Configuration#treat_symbols_as_metadata_keys_with_true_values= is deprecated, it is now set to true as default and setting it to false has no effect.

If you need more of the backtrace for any of these deprecations to
identify where to make the necessary changes, you can configure
config.raise_errors_for_deprecations!, and it will turn the
deprecation warnings into errors, giving you the full backtrace.

1 deprecation warning total

Finished in 0.01258 seconds (files took 0.08262 seconds to load)
4 examples, 1 failure

Failed examples:

rspec ./spec/nested_spec.rb:25 # nested hash #adding_matz operates on the programmer_hash and adds a key/value pair to the top level of the hash, returning the newly-added-to hash

#adding_to_dennis spec should be more specific

Test will pass with the following solution:

programmer_hash[:dennis_ritchie][:languages] = "Assembly"
programmer_hash

However, this replaces the array contents instead of adding to it.
Spec solution:

  describe "#adding_to_dennis" do 
    it "operates on the programmer_hash and adds 'Assembly' to Dennis Ritchie's languages, returning the newly-added-to-hash" do 
      expect(adding_to_dennis[:dennis_ritchie][:languages][1]).to include("Assembly")
    end
  end

#adding_matz has an inconsistency

The comments for #adding_matz state:

   # :languages => ["LISP, C"]

as a modification to the given hash. Looking at the rest of the hash as given, it should read

  # :languages => ["LISP", "C"] 

(the languages are separated as a distinct element of array instead of originally being lumped together as one element. And the test should be updated to reflect that.

RSPEC - Feedback not accurate

Hello I was working on this lab. I had a problem completing this lab because it required me to return the completed hash not just what I added. However this is the feedback I got:

TypeError: no implicit conversion of Symbol into Integer

This was not very informative since this is what I had:
puts programmer_hash[:alan_kay][:known_for]

and I only needed to return the entire hash:
puts programmer_hash

Sorry I submitted this through here but ask a question was not available, was greyed out.

Comment describing Matz's entry has a syntax typo

In nested.rb, there is a comment inside #adding_matz which describes the object representing Matz to add to programmer_hash, but the languages inside the :languages array are in one single string (separated by commas inside the string) instead of as their own strings, separated by commas in the array. See here:

def adding_matz
# add the following information to the top level of programmer_hash
# :yukihiro_matsumoto => {
#   :known_for => "Ruby",
#    :languages => ["LISP, C"]
# }
...

The test for #adding_matz is incomplete.

Hello. The test for #adding_matz checks to see that a :yukihiro_matsumoto key is added to the programmer_hash.

However, according to the comments in that method, the value of programmer_hash[:yukihiro_matsumoto] should be
{
:known_for => "Ruby",
:languages => ["LISP", "C"]
}

The problem is that there is no test to check for that.

Thanks for looking into this issue!
Sdcrouse

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.