Feature #8494

Safe method for defensive copies. alternative to 'dup'

Added by ellipsoid (Andrew Northrop) over 7 years ago.

Target version:


As a developer
I want a simple method that will return a "safe" version of my object
So I can make defensive copies easily and safely

This is related to issue 1844:

I have tried using 'dup' to make defensive copies, but 'dup' throws an exception when used on a Fixnum and the like, because these values aren't copyable.

I tried to throw a "respond_to?" around the call, but that didn't work, since these classes still technically respond to 'dup'.

It looks like there's an extension method to do this (, but I don't see why this isn't in the core language. It seems like a common enough use case.

As far as naming, 'dup' is really just the means to an end. A more meaningful name would be something like 'defensive', or 'safe'. Although 'try_dup' like the extension method would also work.

Any chance of something like this making it in to Ruby core?

No data to display

Also available in: Atom PDF