开发者

How to convert hash in array to array in ruby

开发者 https://www.devze.com 2023-01-07 06:52 出处:网络
I have array that element is hash a = [{:history_date=>\"15/07/10\"}, {:open_price=>\"7.90\"}] 开发者_C百科

I have array that element is hash

a = [{:history_date=>"15/07/10"}, {:open_price=>"7.90"}]
开发者_C百科

I want to convert to this

h = {:history_date=>"15/07/10", :open_price=>"7.90"}

someone please help me.


How about:

h = a.inject(&:merge)

Each hash in the array is merged into the previous hash until we get down to one element. &:merge is shorthand for the following, which may be easier to understand, but is slightly longer:

h = a.inject { |all, element| all.merge(element) }
0

精彩评论

暂无评论...
验证码 换一张
取 消