Ruby的dup和clone方法有什么区别? - What's the difference between Ruby's dup and clone methods?

问题:

The Ruby docs for dup say: 用于dupRuby文档说:

In general, clone and dup may have different semantics in descendent classes. 通常, clonedup在后代类中可能具有不同的语义。 While clone 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章