Episode 18a - Arrays Within Arrays

The values in an array can be more than just numbers or strings. Let's define an array called coordinates, where each item isn't a string or a number, but instead is another array:

coordinates = [[3, 2], [2, 10], [4, 0]]

You can retrieve items in the outer coordinates array as normal using brackets. For example, to retrieve the second item from the coordinates array:

p coordinates[1] # the output is [2, 10]

In order to retrieve items from the sub-array, you would use brackets again. For example, to retrieve the first sub-array item from the second coordinates array item:

p coordinates[1][0] # the output is 2


  1. Start with the array
    coordinates = [[3, 2], [2, 10], [4, 0]]
    Print out the third sub-array, then print out the second item of that sub-array. The result should be:
      [4, 0]
  2. Start with the array
    people = [["Bruce", "Wayne", "Batman"], ["Selina", "Kyle", "Catwoman"], ["Barbara", "Gordon", "Oracle"], ["Terry", "McGinnis", "Batman Beyond"]]
    Use an each loop to print out each person on separate lines with their alter egos. The result should be:
    Bruce Wayne, a.k.a. Batman
    Selina Kyle, a.k.a. Catwoman
    Barbara Gordon, a.k.a. Oracle
    Terry McGinnis, a.k.a. Batman Beyond
  3. Start with the array:
    array = [["a", "b", "z"], ["c", "d"], ["e", "f"], ["g", "h", "i", "j"]]
    Use an each loop within an each loop to take each of those letters and put them into a brand new one dimensional array. The result should be:
    ["a", "b", "z", "c", "d", "e", "f", "g", "h", "i", "j"]
    Note - don't use the flatten method in this case, since it does all the work for you!