I
Ian Trudel
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 <Data:0x2b24088 @name="Account"
@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.
Any suggestions?
Ian
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 <Data:0x2b24088 @name="Account"
@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.
Any suggestions?
Ian