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 clone should do the trick. (Marshalling won't work for some objects... and it makes sense if Procs fall into that category).
clone is 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 of clone
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With