I'd think, the difference in following example shows easier, where is the
difference in you case, too
foo = [1,2]
bar= foo.dup
foo<< 3
puts bar.inspect
foo= [ [1,2] ]
bar = foo.dup
foo[0] << 3
puts bar.inspect
Try it with clone - and you recognize the difference between deep copy
clone and sh
Look the below code with Object#clone:
foo = Object.new
def foo.bar
"hi"
end
baz = foo.clone
foo.bar # => "hi"
baz.bar # => "hi"
Now with Object#dup
foo = Object.new
def foo.bar
"hi"
end
baz = foo.dup
foo.bar # => "hi"
baz.bar # `': undefined method `bar' for #
(NoMethodError)
Why the below
2 matches
Mail list logo