From: ts Date: 2000-08-24T02:42:40+09:00 Subject: [ruby-talk:4570] Re: Q's on Marshal >>>>> "R" == Robert Feldt writes: R> I have an object in an extension that have some Fixnum's and some Bignum's R> and some internal state (in char[1000]). What you can do is create an array with contains : * string (the internal state) * fixnums, bignums Now you can : * return this array and let the user call Marshal#dump * or call Marshal#dump on this object and return the result. R> Anyone got a simple example using _dump and _load in an extension? Well, this example is *completely* stupid, normally tt_restore must return a good object (I've written it very quickly, verify it) pigeon% cat toto.c #include static VALUE tt_mMarshal; static VALUE tt_save(obj, limit) VALUE obj, limit; { VALUE ary; ary = rb_ary_new(); rb_ary_push(ary, rb_str_new("aaabbbb", 7)); rb_ary_push(ary, INT2NUM(12)); rb_ary_push(ary, rb_float_new(24)); return rb_funcall(tt_mMarshal, rb_intern("dump"), 1, ary); } static VALUE tt_restore(obj, a) VALUE obj, a; { VALUE ary; ary = rb_funcall(tt_mMarshal, rb_intern("load"), 1, a); rb_warn("result : %s %d %f", RSTRING(RARRAY(ary)->ptr[0])->ptr, NUM2INT(RARRAY(ary)->ptr[1]), NUM2DBL(RARRAY(ary)->ptr[2])); return Qtrue; } void Init_toto() { VALUE toto, tt; tt_mMarshal = rb_const_get(rb_cObject, rb_intern("Marshal")); tt = rb_define_class("Tt", rb_cObject); rb_define_method(tt, "_dump", tt_save, 1); rb_define_singleton_method(tt, "_load", tt_restore, 1); } pigeon% pigeon% cat b.rb #!/usr/bin/ruby require 'toto' a = Tt.new b = Marshal.dump(a) p b Marshal.load(b) pigeon% pigeon% b.rb "\004\003u:\001\002Tt\001\027\004\003[\001\003\"\001\aaaabbbbi\001\ff\001..." ./b.rb:6: warning: result : aaabbbb 12 24.000000 pigeon% Guy Decoux