I want to use ordered hash.
existing_hash = { key: value1, foo: value2 }
I want add { bar: value3 } after key 'key:'
expecting_hash = { key: value1, bar: value3, foo: value2 }
My code:
existing_hash[:bar] = value3
actual_hash = { key: value1, foo: value2, bar: value3 }
@Sergio is right about unordered nature of
Hash, but technically it looks sorta interesting.Could not refrain from find a solution 🙂
Here it is, though not really elegant:
And applied to your code: