I have a KillerSudoku class in my application.
Every instance has many cells in @cells, many zones in @zones and so on.
Is there a way to easily copy an object (the sudoku) copying all it’s “sub-objects”, that’s to say, I want my copy to have cells and zones I can modify without modifying the previous sudoku?
Assuming that all parts are serializable by
Marshaland you want a completely deep clone:Seen in action:
From the docs, the above won’t work if you have any of the following in your structures:
anonymous
ClassorModule.objects which are related to the system (e.g.
Dir,File::Stat,IO,File,Socket, etc.)an instance of
MatchData,Data,Method,UnboundMethod,Proc,Thread,ThreadGroup,Continuationobjects which define singleton methods
As seen above, this is a truly deep clone, such that even strings become new instances. If you want the arrays of cells and zones to be cloned, but have all values still referencing the same objects, then you want to customize what
dupandclonedo by usinginitialize_copy:Seen in action: