psych does not process hash map with symbol #76

Closed
mcr opened this Issue Aug 7, 2012 · 3 comments

Projects

None yet

2 participants

@mcr
mcr commented Aug 7, 2012

see: plataformatec/devise#2013

I think the fault is psych, I think it should have a .to_s on the o.value at line 21 of psych-1.3.4/lib/psych/visitors/

@tenderlove
Ruby Programming Language member

Sorry, I need more information before I can help with this.

@mcr
mcr commented Aug 8, 2012

Hi, I found the problem, as I indicated in the pullup request: a monkey patch to Hash::encode_with() tries to sort the keys, and the result is disaster when I run it against the test cases in psych. So, how can I sort the keys in my yaml? The major reason to do this is for system level testing of REST APIs...

@tenderlove
Ruby Programming Language member

If you're emitting YAML and you need to sort the keys, it's best just to create a new hash with the sorted keys:

irb(main):001:0> require 'psych'
=> true
irb(main):002:0> x = { 'b' => '1', 'a' => '2' }
=> {"b"=>"1", "a"=>"2"}
irb(main):003:0> puts Psych.dump x
---
b: '1'
a: '2'
=> nil
irb(main):004:0> y = Hash[x.sort_by { |k,| k }]
=> {"a"=>"2", "b"=>"1"}
irb(main):005:0> puts Psych.dump y
---
a: '2'
b: '1'
=> nil
irb(main):006:0>
@tenderlove tenderlove closed this Aug 24, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment