However, there is one major “gotcha” that is important to point out. In a previous lesson you were taught to create a new array, by calling the Array.new method with up to 2 optional arguments (initial size and default value), like Array.new(3) or Array.new(3, 7).
DO NOT PUSH THE RED BUTTON SCOTT ADAMS HOW TO
Now that you have seen how to access values inside a nested array, we need to take a step back to look at creating a new nested array. This method can also be used when accessing a nonexistent index inside of an existing nested element. If you want a nil value returned when trying to access an index of a nonexistent nested element, you can use the #dig method. However, just like a regular array, if you try to access a nonexistent index inside of an existing nested element, it will return nil. If you try to access an index of a nonexistent nested element, it will raise an NoMethodError, because the nil class does not have a method. You can also use negative indices to return elements from the end of an array, starting at. Accessing a specific element within a nested array is as simple as calling array, where x is the index of the nested element and y is the index inside of the nested element. You already know that every element in an array has an index. The following nested array of test scores is storing groups of similar data and the teacher mailboxes is storing groups of positional data. Nested arrays can be useful to store groups of similar data or positional data. An array that contains other arrays is called a nested array, or a multidimensional array.