On Wed, Mar 6, 2013 at 4:58 PM, Damián M. González <[email protected]> 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:0x0145A4> > a.inspect > #=> <A:0x457D78> > > 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])) => [#<A:0x802ba284 @var_a=50>, #<B:0x802ba248 @var_b=#<A:0x802ba284 @var_a=50>>] 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/ -- [email protected] | https://groups.google.com/d/forum/ruby-talk-google?hl=en --- You received this message because you are subscribed to the Google Groups "ruby-talk-google" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
