Marshal retains an idea of identity only during one write operation.
So it will reconstruct graphs properly if you ensure all interesting
instances are referenced from a single instance.
I can now clearly see what you say.
As Hans said: do it in one step for example by placing both in an Array:
irb(main):016:0> x, y = Marshal.load(Marshal.dump([a,b]))
=> [#<A:0x802ba284 @var_a=50>, #<B:0x802ba248 @var_b=#<A:0x802ba284
@var_a=50>>]
irb(main):017:0> x.equal? y.var_b
=> true
Yes, it’s what I say above
In your case it would also be sufficient to only marshal b because
that references a already and there is an easy way to access it after
deserialization.
I understand what you say, but my model is far more complex, I have
many objects with atributtes pointing to anothers objects from
differents clases, it’s not so simple. At now the only way that I see
is: in some way try to reconstruct the objects manually, for example,
many of my classes will have only one instance at time in the model, so
I can do something like this(referencing the above example):
---------------------------------*-
#supposing that before I’ve loaded the state of ::A1 which is the state
#of the above a variable
#monkeypatch
class B
def marshal_dump
[@var_b]
end
#Note: I have all the only-one-instance-per-class pointed by a
#constant in the main scope(don’t know if this is a good way just
#implemented in that way))
def marshal_load(attributes)
@var_b = (if attributes[0].is_a?(A) then ::A1 else attributes[0] end)
end
end
-----------------------------------*-
Do you think that exist a better and faster way than this to treat with
the problem? I’ll appreciate your help, thanks.