Creating array of hashes in ruby
arrays, hashmap, ruby
Solution
Assuming what you mean by "rowofrows" is an array of arrays, heres a solution to what I think you're trying to accomplish:
array_of_arrays = [["abc",9898989898,"abc@xyz.com"], ["def",9898989898,"def@xyz.com"]]
array_of_hashes = []
array_of_arrays.each { |record| array_of_hashes << {'name' => record[0], 'number' => record[1].to_i, 'email' => record[2]} }
p array_of_hashes
Will output your array of hashes:
[{"name"=>"abc", "number"=>9898989898, "email"=>"abc@xyz.com"}, {"name"=>"def", "number"=>9898989898, "email"=>"def@xyz.com"}]
Problem
I want to create an array of hashes in ruby as: ``` arr[0] "name": abc "mobile_num" :9898989898 "email" :abc@xyz.com arr[1] "name": xyz "mobile_num" :9698989898 "email" :abcd@xyz.com ``` I have seen hash and array documentation. In all I found, I have to do something like ``` c = {} c["name"] = "abc" c["mobile_num"] = 9898989898 c["email"] = "abc@xyz.com" arr << c ``` Iterating as in above statements in loop allows me to fill `arr`. I actually rowofrows with one row like `["abc",9898989898,"abc@xyz.com"]`. Is there any better way to do this?