From: Paul Brannan Date: 2004-09-08T23:21:21+09:00 Subject: Re: Marshal and allocating objects On Sat, Sep 04, 2004 at 07:45:22PM +0900, Stephen Sykes wrote: > I would find it useful if Marshal were to use "allocate" to generate > new objects. It does use allocate, for objects of type T_DATA (objects created from C extensions). > This is so that I can load unknown objects by doing something like > this: > > class Object > def self.const_missing(name) > const_set(name, kl = Class.new) > kl > end > end > > And then be able to load objects with marshal without getting > ArgumentError: undefined class/module. Even if allocate is used, this won't work. The lookup for the class is failing before allocate is called. > For example (after running the above code)... > > irb(main):026:0> M.allocate > => # > > That works ok, but... > > irb(main):029:0> Marshal.load("\004\010o:\006N\000") > ArgumentError: undefined class/module N > from (irb):29:in `load' > from (irb):29 > from (null):0 > > If Mashal used allocate, and hence called const_missing, then the load > would not fail. Sound reasonable? I don't like your solution, since now if I write: class Foo SOME_CONSTANT = 1 def foo return SOME_CONSTATN end end my code will no longer break, but will silently do the wrong thing. Arguably better would be a callback for when Marshal.load cannot find the class it needs. I say arguably because there really is no good way to create a class with the correct behavior unless you already know something about the class. Paul