I have this array of hashes:
- :name: Ben :age: 18 - :name: David :age: 19 - :name: Sam :age: 18
I need to group them by age
age
The &:age means that the group_by method should call the age method on the array items to get the group by data. This age method is not defined on the items which are Hashes in your case.
&:age
group_by
This should work:
array.group_by { |d| d[:age] }