Create a hash from an array using group_by

I have the following array

ages = [["a", 15],["b", 16], ["c", 15], ["d", 16], ["e", 17], ["f", 20]]

I have to create a hash with ages as values such that it looks like this

{15 => ["a","c"], 16=> ["b","d]....}

when I run the group_by method:

puts ages.group_by {|list| list[1]}

this is what i get:

{15=>[["a", 15], ["c", 15]], 16=>[["b", 16], ["d", 16]], 17=>[["e", 17]], 20=>[["f", 20]]}

I'd really appreciate it if you can clarify how to make this cleaner and get the values as an array of names with same ages.



from Recent Questions - Stack Overflow https://ift.tt/3ktTccb
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)