I have a use-case where I have an existing hash:
response = { aa: 'aaa', bb: 'bbb' }
I need to add id as one of the keys.
When I use response.merge(id: 'some_id') and then convert it into JSON, I got id as the last element, which I don't want.
I want to insert id: 'some_id' at the beginning of response.
I have tried this, but it doesn't feel good to iterate over it:
new_response = { id: 'some id' }
response.keys.reverse.each {|key| new_response[key] = response[key] }
Basically, I need a similar feature like Ruby Array's unshift.
irb(main):042:0> arr = [1, 2, 3]
=> [1, 2, 3]
irb(main):043:0> arr.unshift(5)
=> [5, 1, 2, 3]