I ended up accidentally doing the equivalent of this in Ruby the other night:
a = *1..5 # => [1, 2, 3, 4, 5]
a << a
a # => [1, 2, 3, 4, 5, [...]]
a.last # => [1, 2, 3, 4, 5, [...]]
What is [...]
and what can I do with it?
The default Ruby implementation of the #dup method allows you to add a special initializer to your object that is only called when an object is initialized via the #dup method. These methods are: initialize_copy.
Ruby does provide two methods for making copies of objects, including one that can be made to do deep copies. The Object#dup method will make a shallow copy of an object. To achieve this, the dup method will call the initialize_copy method of that class. What this does exactly is dependent on the class.
It's just the way Array.inspect displays recursive arrays. The last Element of a is a itself. If a where displayed after 5, inspect would end up in an endless loop:
[1, 2, 3, 4, 5, [1, 2, 3, 4, 5, [1, 2, 3, 4, 5, [1, 2, 3, 4, 5, [...]]]]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With