From: Robert Klemme Date: 2013-03-07T01:33:08+09:00 Subject: Re: Persisting and loading objects trhough Marshal problem On Wed, Mar 6, 2013 at 4:58 PM, Damián M. González wrote: > Good question. See, I marshal them in differents times. Because I don't > have to persist lot of information I don't use any database like MySQL, > I just save the object in binary files(by marshaling). The application > have a GUI, so when a user do some thing certain object is persisted, > that object is pointing to other objects, like the example of @var_b. > The problem is that when I regenerate the objects, the shown above > happen. > When in some part of the source code I have to compare in the way above > the are not equals: > > b.var_b.inspect > #=> > a.inspect > #=> > > They are not the same, perhaps(meaby an obvious thing) because Marshal > reconstruct the object just by looking their atributes saved but Marshal > doesn't bother about create the same object that another that will be > created with the same attributes. Am I right? 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. 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])) => [#, #>] irb(main):017:0> x.equal? y.var_b => true 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. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/