Returns a hash, which keys are evaluated result from the block, and values are arrays of elements in enum corresponding to the key.
(1..6).group_by {|i| i%3} #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
This is included in Ruby 1.9 www.ruby-doc.org/core-1.9/classes/Enumerable.html#...
Implementation from Ruby on Rails: trunk/activesupport/lib/active_support/core_ext/enumerable.rb [rev 5334]
Source Code
# File merb/core_ext/enumerable.rb, line 43 def group_by inject({}) do |groups, element| (groups[yield(element)] ||= []) << element groups end end
<code/>and<pre/>for code samples.