Clone doesn’t clone the sub-objects that the main object refers to
unless they are simple literals (like Integers). So for your second
example, the array has references to the sub arrays and when it clones,
it just clones the values of those references, but they still refer to
the same arrays and when you change an index of the clones sub arrays,
it changes the original ones.
On Thu, Nov 27, 2014 at 10:44 AM, Robert K. [email protected] wrote:
ones.
In case you want to dig a bit deeper, you’ll find more explanations
under keywords “shallow copy”, “deep copy” and “aliasing”.
Another note: it can be misleading to think in terms of
multidimensional arrays. Ruby doesn’t have those, it just has Arrays
of Arrays. On the object level, there’s no different between those and
a regular Array of any other object.
Clone doesn’t clone the sub-objects that the main object refers to unless
they are simple literals (like Integers). So for your second example, the
array has references to the sub arrays and when it clones, it just clones
the values of those references, but they still refer to the same arrays and
when you change an index of the clones sub arrays, it changes the original
ones.
In case you want to dig a bit deeper, you’ll find more explanations
under keywords “shallow copy”, “deep copy” and “aliasing”.