On Tue, May 3, 2011 at 7:08 PM, John Feminella <johnf / bitsbuilder.com> wrote:
> Your example doesn't contain nested hashes, while mine does.

Neither did Peter's example and request.

> That's what I was demonstrating -- it's a shallow copy, not a deep
> one.

Simply using #dup or #clone meets the original request as presented,
and does so for arbitrary Ruby objects in the hashes.

If the Peter needs a deep copy, which Peter didn't ask for (just a
copy of the data in each hash in the array -- not nested hashes -- so
that the originals in the area wouldn't be modified on iteration) its
true that simply using #dup or #clone won't work.

OTOH, if Peter needs to be able to handle arbitrary Ruby objects in
the hashes, using Marshal.load(Marshal.dump(whatever)) won't work,
since there are objects that can't be dumped.

What if the values in the hash are lambdas?

The load/dump mechanism meets some other need, but doesn't meet the
original request for hashes with arbitrary data (and is an
extraordinarily convoluted mechanism for meeting the original request
even where it works.)

Its possible to craft a generic deep-copying mechanism, if you need
it, but Marshal.load(Marshal.dump(whatever)) isn't it.