Hash#to_s (alias for inspect) always outputs data in the 1.8 hash style:
{key: "value"}.to_s
=> "{:key=>\"value\"}"
Is there any core method that will serialize it in the 1.9 hash style?
{key: "value"}.to_s
=> "{key: \"value\"}"
I'm using this on known data for code refactoring; and since Ruby tends to have an implementation of everything, I'm hoping I was just looking in the wrong place.
Of course, you could hack it in an ugly fashion
"{" + my_hash.to_a.map{|pair| pair[0].to_s + ": " + pair[1].inspect} * ",\n") + "}"
But I'm hoping there's a core method somewhere that's unit tested and fully correct.
This works for me ... and it respects string keys.
{key: "value"}.to_s.gsub(/(:(\w+)\s?=>\s?)/, "\\2: ")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With