Deep clone

I need to produce deep copies of an object structure (the elements will
only be hashes, arrays, symbols, strings, numbers, true/false, nils; and
not “complex” stuffs like File object or database connection handles).
Will this do fine?

def deep_clone(obj)
Marshal.load(Marshal.dump(obj))
end

Any gotchas I should know?

···


dave

David Garamond wrote:

I need to produce deep copies of an object structure (the elements will
only be hashes, arrays, symbols, strings, numbers, true/false, nils; and
not “complex” stuffs like File object or database connection handles).
Will this do fine?

def deep_clone(obj)
Marshal.load(Marshal.dump(obj))
end

Any gotchas I should know?

That approach seems to be the standard answer. See for example:

http://ruby-talk.org/3534

All the datatypes you mention can be handled by Marshal.