From: Jim Freeze Date: 2001-05-18T02:27:11+09:00 Subject: [ruby-talk:15334] How do I copy an object? Hi: I want to copy an object that contains an array. I want the new object to have its own array, not a pointer to the original array. How is this done? I have tried direct asignment, .dup and .clone, but none work. Here is some sample code: class Stmt def initialize @s = [] @s << "text 1" @s << "text 2" end def []=(i,val) @s[i] = val end def [](i) @s[i] end end def func(s) locals = s.dup # have tried s, s.dup and s.clone -----------^^^^^ # How do I get a local copy of object s? locals[0] = "local change" end ## main s = Stmt.new puts s[0] func(s) puts s[0] ## end#main With output: text 1 local change Thanks ========================================================= Jim Freeze jim@freeze.org --------------------------------------------------------- No comment at this time. http://www.freeze.org =========================================================