[wxruby-users] use of Marshal with wxruby classes
Alex Fenton
alex at pressure.to
Fri Jan 4 21:21:35 EST 2008
Robert Anderson wrote:
> Is it possible to use Marshal with wxruby classes to serialize an
> application's state?
>
> I tried a minimal example of simply serializing a minimal frame
> object, and I get the error "no marshal_dump is defined for class
> MinimalFrame".
>
> I don't fully understand this because I also tried another minimal
> example of dumping a class which did not defined a marshal_dump, and
> it worked fine. Is there some reason this has been disabled in wxruby
> classes? Sorry, I picked up ruby a few days ago so I may be missing
> something obvious.
It's not completely obvious, but I'm fairly sure what's happening is
this: Marshal is able to store ordinary ruby objects that are composed,
in the end, of core ruby types, like Float, Fixnum, String, Hash etc. So
if you write any class just in ruby, Marshal can serialise its instances.
Objects like a Wx::Frame, or any other ruby object that comes from a
ruby C extension is different. It's a wrapper or pointer to compiled
code (a C++ object in wxRuby). Ruby can't serialise and restore such
objects without extra information.
Not sure why you'd want to serialise a Wx::Frame. If you're interested
in storing and restoring the size, position or layout of a Frame between
runs, just store that information and a hash and serialise it, and then
when the "same" window is re-opened, Marshal.load the hash and restore
the settings. The first few methods in this real-life app do this sort
of thing:
http://weft-qda.rubyforge.org/svn/trunk/weft-qda/lib/weft/wxgui/inspectors.rb
hth
alex
More information about the wxruby-users
mailing list