問題:
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