From: Chris Hayes on
> Marshal.load(Marshal.dump(array))

I'm curious, has anybody done any benchmarking of this method?

It seems to me this method would have the same kind of overhead &
performance hit as PHP's unserialize(serialize(object)).

I had a similar issue with deep copying an array just now...

Instead of using Marshal, I just extended Array with a roll-my-own:

class Array
def dclone
a = []
each { | x | a << x.dclone }

I think this is a much more robust solution, as it avoids added overhead
that may be hiding inside dump/load, and allows for the deep clone to
further use the children's deep clone methods.

I *believe* this is what the developers intended, as dclone begins as
simply an alias for clone (for basic objects, there's no need to go
deeper. Clone something with no children, and dclone *will* behave
exactly like clone).

This is of course only valid where dclone is defined (likely depends on
the version of ruby you're using). You can modify the above code to be
something more like this:

class Array
def dclone
a = []
each do | x |
if x.respond_to? :dclone
a << x.dclone
a << x.clone

I'm avoiding my usual habit of using the ? : trinary operator, for
clarity, here.
Posted via