I am very new to Ruby array and hash manipulation.
How can I do this simple transformation?
array = [#<struct id=1, car='red'>, #<struct id=2, car='yellow'>, #<struct id=3, car='green'>]
desired output in json:
[{id : 1, car : 'red'} , {id:2, car :'yellow'} ,{id:3 , car: "green"}]
Does anyone have any hints?
array.map { |o| Hash[o.each_pair.to_a] }.to_json
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