method
group_by
v2_6_3 -
Show latest stable
- Class:
Enumerable
group_by()public
Groups the collection by result of the block. Returns a hash where the keys are the evaluated result from the block and the values are arrays of elements in the collection that correspond to the key.
If no block is given an enumerator is returned.
(1..6).group_by { |i| i%3 } #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
1Note
group on hash
'
def group_by_hash hash, value
hash.group_by do |k,v|
v > value ? "Big" : "Small"
end
end
marks = {"Chair" => 30, "Table" => 40, "Bed" => 60, "stool" => 20}
group_by_hash(marks, 30)