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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章