Arrays of Arrays: Referencing
Learn how to make different references to arrays so that they point to different objects in Ruby.
We'll cover the following...
Referencing
The answer to why all the array elements get updated when we modify one element of the array can be found in the concept of a reference. When initializing an array, we pass the reference to a single object:
arr = Array.new(10, 'something')
something
above is a String
object, since everything is an object in Ruby. Since we’re passing the reference to this single object, the array will get initialized with 10 cells, each with the same reference value. In other words, there is no object inside a cell. There is only a reference to an object.
To avoid this side effect, we need these references to be different, so that they point to different objects. These objects will be placed at different locations in computer memory, but technically they will look the same.
It’s like having the same type of beer in your six-pack: all the bottles look the same, but they’re all different bottles. If we change the state of one bottle, it won’t affect the state of the other bottles.
With the example of an apartment complex with multiple doorbells, imagine the following scenario. We brought a box—an array—and we want to put 10 doorbells inside that box. We did that, but all the wires led to only one specific apartment. It doesn’t matter which doorbell we use, because we’ll only get an answer from the same tenants of that one apartment.
The right way
If we want to fix this mistake, we will need these wires to lead to different apartments. Always avoid code like this:
arr = Array.new(10, []) # <-- WRONG!
It’s wrong because the array inside is supposed to change its state. Why would we need an empty array? There is no sense in that, because we’ll eventually want to add something to that empty array. That is exactly what arrays were created for. But with strings, things are easier, and the following code works:
arr = Array.new(10, 'something')
But with one caveat: We will not use a “dangerous” operation on a string or any other data type. A ...