From: why the lucky stiff Date: 2003-08-29T01:51:14+09:00 Subject: Re: YAMLand rbtree, was: Re: sorting on hash contents On Thursday 28 August 2003 02:27 am, Joel VanderWerf wrote: > I notice that rbtree works with Marshal, but not YAML. What methods > would have to be added to rbtree to fix this? Currently, RBTree would need to inherit from Hash (or) introduce custom is_complex_yaml?, to_yaml_type and to_yaml methods. Here's custom methods which appear to work: # Emitting the class class RBTree def to_yaml_type "!ruby/rbtree" end def to_yaml( opts = {} ) YAML::quick_emit( self.object_id, opts ) { |out| out.map( to_yaml_type ) { |map| map.concat( self.to_a ) } } end end # Loading the class YAML.add_ruby_type( 'rbtree' ) { |type, val| r = RBTree.new val.each { |k, v| r[k] = v } r } # Test a = RBTree['ducks', 100, 'bears', 12, 'badgers', 0] puts a.to_yaml p YAML::load( a.to_yaml ) # Looks like... --- !ruby/rbtree badgers: 0 bears: 12 ducks: 100 I'm not very happy with this technique though. I'd rather it was simpler, such as: class RBTree include YamlHashMixin end _why