What is the difference between Object#dup and #clone ?
What is the difference between Object#dup and #clone ?
The difference between Ruby's Object#dup and Object#clone methods primarily lies in how they handle the object's frozen state, singleton methods, and taint status.
Frozen State: Object#clone copies the frozen state of the original object. If the original object is frozen, the cloned object will also be frozen. On the other hand, Object#dup does not preserve the frozen state; the duplicated object will be unfrozen even if the original object was frozen.
Singleton Methods: Singleton methods (methods defined on an individual object rather than on the class of the object) are copied by Object#clone but not by Object#dup. This means that if you define singleton methods on an object and then clone it, the cloned object will also have these singleton methods. However, if you duplicate the object using Object#dup, the new object will not have the singleton methods.
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.