tags:

views:

122

answers:

1

Hi,

I need a universal cloning solution to deep clone module with all it's instance variables in ruby. I can't really use .clone method, since it doesn't work on activerecord objects (doesn't copy the id field). I also saw a workaroung by using marshal dump + marshal load, but it doesn't work on module and singleton objects. Does anyone know any solution to module? Or maybe you have another solution for my real problem: I have a module with instance variables set and I need to clone it to another module, modify objects in cloned object, then clone first module again without having changes made by second module.

A: 

You can use dup instead of clone for ActiveRecord objects, but dup doesn't copy singleton methods(defined on objects) + it changes frozen state to tainted one.

As for Rails also you have the Deep cloning plugin for ActiveRecord cloning.

khelll