问题:
The Ruby docs for dup
say: 用于dup
的Ruby文档说:
In general,
clone
anddup
may have different semantics in descendent classes. 通常,clone
和dup
在后代类中可能具有不同的语义。 Whileclone
is used to duplicate an object, including its internal state,dup
typically uses the class of the descendent object to create the new instance. 虽然clone
用于复制对象,包括其内部状态,但dup
通常使用后代对象的类来创建新实例。
But when I do some test I found they are actually the same: 但是当我做一些测试时,我发现它们实际上是相同的:
class Test
attr_accessor :x
end
x = Test.new
x.x = 7
y = x.dup
z = x.clone
y.x => 7
z.x => 7
So what are the differences between the two methods? 那么这两种方法有什么区别?
解决方案:
参考一: https://en.stackoom.com/question/gj9u参考二: https://stackoom.com/question/gj9u