Workaround for YAML bug with complex map keys?

Hi,

I’m looking for a workaround for the problem that you can’t
roundtrip a Hash with an OrderedHash as a key. This is probably due to
bug http://redmine.ruby-lang.org/issues/1331. The simplest
demonstration,
using Ruby Enterprise 2011.03 (1.8.7 p334), ActiveSupport 3.0.4:

foo = {ActiveSupport::OrderedHash[‘x’, ‘y’] => ‘z’}
=> {#<OrderedHash {“x”=>“y”}>=>“z”}

YAML.dump(foo)
=> “— \n!omap ? \n - x: y\n: z\n\n”

The YAML that was dumped here is wrong. It should have been:
“— \n? !omap \n - x: y\n: z\n\n”

To demonstrate:
YAML.load("— \n!omap ? \n - x: y\n: z\n\n")
ArgumentError: syntax error on line 2, col -1: ` - x: y
: z

YAML.load("— \n? !omap \n - x: y\n: z\n\n")
=> {#<OrderedHash {“x”=>“y”}>=>“z”}

Does anyone know a workaround
for this. I’ve been looking into the sources, but I fear it will take me
a while to figure it all out and perhaps someone here has a solution at
hand? http://www.ruby-forum.com/topic/215047 may be related

Edit: This problem does not occur in JRuby 1.5.6, but JRuby has it’s own
YAML library, so that makes sense.

bump

Someone must surely have worked around this already? I’ve currently
resorted to changing the internal datastructure of the object where I
needed it from an OrderedHash to an Array of two-element Arrays, but
it’s a bit of a stopgap.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs