Skip to content

Instantly share code, notes, and snippets.

@MilanGrubnic70
Created April 19, 2014 18:33
Show Gist options
  • Save MilanGrubnic70/11093173 to your computer and use it in GitHub Desktop.
Save MilanGrubnic70/11093173 to your computer and use it in GitHub Desktop.
Nested / Multidimensional Arrays
things = [[1,2,3], ["red", "blue"]] #=> create an array of arrays.
puts things[0][1] #=> Print out 2. The array with numbers is at index zero in 'things' and 2 is at index one in that sub-array.
things.each do |sub_array| #sub_array is each element of the 'things' array.
sub_array.each do |item| #The each method is called on the sub-arrays. First [1,2,3] then [1,2,3]
puts item
end
end
# Then, we iterate through the things array and use the variable sub_array for the array containing numbers
# and the array containing colors.
# Next, we iterate through each sub_array and puts out each item.
#=>
1
2
3
red
blue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment