From: Joel VanderWerf Date: 2006-02-08T05:39:16+09:00 Subject: Re: marshalling constants Brian Buckley wrote: > Hello, > > When one marshals a constant and later unmarshals it, a brand new object > appears to be created during unmarshalling. This seems wasteful, especially > in cases when constants are large (contain many attributes). Is it possible > to customize marshal for a class (or for singleton objects of the class) so > that unmarshalling a constant simply returns the already existing constant > rather than creating a new one? I've noticed Fixnum's exhibit this > behavior. > > --Brian > > class A; > A1 = A.new > end > > before = A::A1 > after = Marshal.load(Marshal.dump(before)) #marshal and back > puts before == after #darn -- it is false > puts before.object_id == after.object_id #same here class A A1 = new def self.name_for_constant obj @name_for_constant ||= {} @name_for_constant[obj] ||= constants.find {|c| const_get(c) == obj} end def _dump(limit) self.class.name_for_constant(self) end def self._load(str) const_get str end end before = A::A1 after = Marshal.load(Marshal.dump(before) puts before == after # ==> true puts before.object_id == after.object_id # ==> true -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407