Module: Enumerable
- Defined in:
- lib/nanoc3/extra/core_ext/enumerable.rb
Instance Method Summary collapse
-
#group_by {|obj| ... } ⇒ Hash
Returns a hash, which keys are evaluated result from the block, and values are arrays of elements in enum corresponding to the key.
Instance Method Details
#group_by {|obj| ... } ⇒ Hash
Returns a hash, which keys are evaluated result from the block, and values are arrays of elements in enum corresponding to the key. This method is provided for backward compatibility with Ruby 1.8.6 and lower, since #group_by is only available in 1.8.7 and higher.
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/nanoc3/extra/core_ext/enumerable.rb', line 20 def group_by groups = {} each do |item| key = yield(item) groups[key] ||= [] groups[key] << item end groups end |