From: Michael Fellinger Date: 2009-03-03T02:32:30+09:00 Subject: Re: Marshal.load does not create new instances? On Sat, Feb 28, 2009 at 8:56 AM, Ian Trudel wrote: > Marshal does not seem to instantiate given class(es) on load. Moreover, > it will absolutely work as long as the class is define but even if it > has no attributes. The following code snippet creates an array with > filled with objects: > > class Data > end > > data = File.open("data.bin", "rb") { |f| Marshal.load(f) } > > This absolutely works fine even if the Data class used to dump (e.g. > from another program) has many instance variables. They just won't be > accessible directly (but still can be accessible using reflection). > Inspect would show something like @expanded=true ..>. Furthermore, it doesn't seem to use the defined > class if an IO is fed in Marshal.load, thus overriding Data._load just > won't work. Is that the expected behaviour? > > This is really annoying considering that I would like to load data onto > one (among many) version of Data class. The version of the class to be > used is determined and set at run-time according to the file loaded; > this is my ultimate goal. There is obviously no cast feature in Ruby. > Even using a proxy won't cut it, if only for the fact that Marshal > doesn't instantiate loaded data. http://eigenclass.org/R2/writings/extprot-vs-ruby-marshal ^ manveru