Workaround for ActiveSupport::OrderedHash YAML bug

Discussion in 'Ruby' started by Ivo Wever, Mar 23, 2011.

  1. Ivo Wever

    Ivo Wever Guest


    I'm looking for a workaround for the problem that you can't
    roundtrip a Hash with OrderedHashes as keys. 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"}

    => "--- \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"}

    This issue is probably related to 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
    Ivo Wever, Mar 23, 2011
    1. Advertisements

  2. Ivo Wever

    Ivo Wever Guest


    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.
    Ivo Wever, Mar 28, 2011
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.