I am not sure about the meaning of "...but not the objects they reference" in both the documantion of ruby
and rubinus
.
In ruby-doc, there is the explanation of #clone
and #dup
behavior saying:
Produces a shallow copy of obj—the instance variables of obj are copied, but not the objects they reference. Copies the frozen and tainted state of obj. See also the discussion under Object#dup.
The same is repeated in the implementation of Rubinius:
Copies instance variables, but does not recursively copy the objects they reference. Copies taintedness.
I tried out with the following code, but the behavior is out of my expectation.
class Klass
attr_accessor :array
end
s1 = Klass.new
ar = [1, 2, 3]
s1.array = [ar]
s2 = s1.clone
# according to the doc,
# s2.array should be initialized with empty Array
# however the array is recursivley copied too
s2.array.equal? s1.array # true