Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deep copy of collection containing proc objects

Tags:

ruby

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?

like image 341
ExternalReality Avatar asked Nov 21 '25 22:11

ExternalReality


1 Answers

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

like image 170
RyanWilcox Avatar answered Nov 24 '25 22:11

RyanWilcox