elbows
11/3/2003 3:08:00 AM
Thanks, that was exactly what I needed.
Nathan
ts <decoux@moulon.inra.fr> wrote in message news:<rfcllr0w7dp.fsf@moulon.inra.fr>...
> >>>>> "N" == Nathan Weston <elbows@spamcop.net> writes:
>
> N> I need to marshal and load cyclic data structures, and I need to write
> N> custom marshalling routines because not all the fields of these
> N> structures can be saved.
>
> If you have a recent version of ruby you can use #marshal_load and
> #marshal_dump, something like
>
> svg% cat b.rb
> #!/usr/bin/ruby -v
> class Cons
> def marshal_load(hash)
> @value = hash["value"]
> @rest = hash["rest"]
> end
>
> def marshal_dump
> {"value" => @value, "rest" => @rest}
> end
>
> attr_accessor :value, :rest
> end
>
> first = Cons.new
> first.value = 12
> second = Cons.new
> second.value = 24
>
> first.rest = second
> second.rest = first
>
> c = Marshal.load(Marshal.dump(first))
>
> p first
> p c
> svg%
>
> svg% b.rb
> ruby 1.8.0 (2003-08-04) [i686-linux]
> #<Cons:0x400999e4 @value=12, @rest=#<Cons:0x400998cc @value=24, @rest=#<Cons:0x400999e4 ...>>>
> #<Cons:0x400997f0 @value=12, @rest=#<Cons:0x400997a0 @value=24, @rest=#<Cons:0x400997f0 ...>>>
> svg%