I need to make a deep copy of an object.
The only way I know to make a deep copy
of an object is with the following:
Marshal.load(Marshal.dump(my_object))
To my dismay, I found that if some element
of the object being deep copied is a proc object
then I get an error because proc objects don’t
have a dump method and can’t be deep copied that
way.
How do I make a deep copy of an object with
procs in them?
A deep copy in Ruby using
cloneshould do the trick. (Marshalling won’t work for some objects… and it makes sense if Procs fall into that category).cloneis a convention that means deep copy, even though deep copies aren’t supported in Ruby out of the box. However, an answer on SO to a similar question has a really good, generic, implementation ofclone