In short, there is no way to do this without some serious hacking. What I suggest you to do is to make a to_serialized
method that returns an array that the initialize
method accepts to get the same state. If you simply want to copy all instance variables over, you could do this:
class A
def initialize(instance_variables)
instance_variables.each do |key, value|
self.instance_variable_set(key, value)
end
end
def to_serialized
iv = {}
self.instance_variables.each do |key|
iv[key] = self.instance_variable_get(key)
end
end
end
And to reload the method, you could do this:
obj_state = object.to_serialized
Object.remove_const('A')
load 'file.rb'
object = A.new(obj_state)
Note that this doesn't nest, so if any of the objects the instance variables refer to is reloaded too, you need to "serialize" them yourself.