Making a Copy of an Object – Ruby

April 19, 2008 – 1:11 pm

You want to make a copy of an existing object: a new object that can be modified separately from the original, Use Object#clone – makes a copy of the metaclass and instantiates the copy, instead of instantiating the object’s original class

example how to use clone :

s1 = 'foo'                             # => "foo"
s2 = s1.clone                          # => "foo"
s1[0] = 'b'
[s1, s2]                               # => ["boo","foo"]
Related Posts with Thumbnails

Tags: ,


Post a Comment