Feature #8494

Safe method for defensive copies. alternative to 'dup'

Added by Andrew Northrop over 2 years ago.



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: http://www.ruby-forum.com/topic/3650325

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 (http://ruby-doc.org/gems/docs/c/core_ex-, 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?

Also available in: Atom PDF