Giter VIP home page Giter VIP logo

phase-3-nested-arrays-addendum's Introduction

Nested Arrays Addendum

Learning Goals

  • Recognize that coordinate assignments grow nested Arrays
  • Recognize single-coordinate access of Arrays returns an Array

Introduction

Now that you've had a chance to get familiar with the basics of working with nested Arrays or "matrices" (the plural of "matrix"), or "AoAs," let's cover a "funny" case.

Recognize That Coordinate Assignments Grow Nested Arrays

Given the following Ruby code:

spice_rack = [
  ["Mace", "Ginger", "Marojam"],          
  ["Paprika", "Fajita Mix", "Coriander"], 
  ["Parsley", "Sage", "Rosemary"]         
]

You're clear now that we can update by saying spice_rack[1][1] = "Extract of Pizza". But what would happen if we said spice_rack[1][100] = "Poodle Dinner". What would spice_rack look like then?

TRY IT OUT IN IRB. We can't emphasize this behavior enough. Programmers, when they reach head-scratchers like this, always ask IRB to teach them. Your learning will not gel as well if you do not take this step. Don't set your learning backwards!

As we hope you discovered, we can add at any coordinate pair. If the element you need add needs the inner Array to "grow" in order to accommodate it, Ruby will "grow" the Array and fill in the "in-between" values with nil.

spice_rack = [
  ["Mace", "Ginger", "Marojam"],             
  ["Paprika", "Fajita Mix", "Coriander"],    
  ["Parsley", "Sage", "Rosemary"]            
]
spice_rack[1][10] = "Cucumber Water"
spice_rack #=> [
  ["Mace", "Ginger", "Marojam"],
  ["Paprika", "Fajita Mix", "Coriander", nil, nil, nil, nil, nil, nil, nil, "Cucumber Water"],
  ["Parsley", "Sage", "Rosemary"]
]

Recognize Single-coordinate Access of Arrays Returns an Array

This isn't a new fact, but some students forget that the inner Arrays are still Arrays upon which we can do all the usual stuff we already know how to do on Arrays.

As such, you can use operators like << ("shovel") on an inner Array:

spice_rack = [
  ["Mace", "Ginger", "Marojam"],          
  ["Paprika", "Fajita Mix", "Coriander"], 
  ["Parsley", "Sage", "Rosemary"]         
]

spice_rack[2] << "Saffron" #=> ["Parsley", "Sage", "Rosemary", "Saffron"]

spice_rack #=> [
  ["Mace", "Ginger", "Marojam"],          
  ["Paprika", "Fajita Mix", "Coriander"], 
  ["Parsley", "Sage", "Rosemary", "Saffron"]         
]

Similarly, if you want to replace a whole Array within the containing Array, you can do so by using one coordinate.

spice_rack = [
  ["Mace", "Ginger", "Marojam"],          
  ["Paprika", "Fajita Mix", "Coriander"], 
  ["Parsley", "Sage", "Rosemary"]         
]

# Spice up your life!
spice_rack[0] = ["Posh", "Scary", "Sporty", "Baby", "Ginger"]

spice_rack #=> [
  ["Posh", "Scary", "Sporty", "Baby", "Ginger"],
  ["Paprika", "Fajita Mix", "Coriander"],
  ["Parsley", "Sage", "Rosemary"]
]

Keep in mind, if a matrix starts off with the same number of rows and elements ("is square"), there's nothing wrong with breaking that, if you need. Ruby won't complain.

Conclusion

We've covered two "tangent" topics about working with nested arrays. We've seen these ideas occasionally cause bugs for learners ("Wait, what? How did all those nils get in there?"). Or we've seen learners get stuck because they stop thinking of inner Arrays as the Arrays they already know and love. They get all tangled up in the coordinate syntax and forget what they already know.

phase-3-nested-arrays-addendum's People

Contributors

ihollander avatar maxwellbenton avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.