Module: Enumerable
- Defined in:
- lib/active_support/core_ext/enumerable.rb
Overview
:nodoc:
Instance Method Summary collapse
- #first_match ⇒ Object
-
#group_by ⇒ Object
Collect an enumerable into sets, grouped by the result of a block.
Instance Method Details
#first_match ⇒ Object
2 3 4 5 6 7 8 |
# File 'lib/active_support/core_ext/enumerable.rb', line 2 def first_match match = nil each do |items| break if match = yield(items) end match end |
#group_by ⇒ Object
Collect an enumerable into sets, grouped by the result of a block. Useful, for example, for grouping records by date.
e.g.
latest_transcripts.group_by(&:day).each do |day, transcripts|
p "#{day} -> #{transcripts.map(&:class) * ', '}"
end
"2006-03-01 -> Transcript"
"2006-02-28 -> Transcript"
"2006-02-27 -> Transcript, Transcript"
"2006-02-26 -> Transcript, Transcript"
"2006-02-25 -> Transcript"
"2006-02-24 -> Transcript, Transcript"
"2006-02-23 -> Transcript"
25 26 27 28 29 30 |
# File 'lib/active_support/core_ext/enumerable.rb', line 25 def group_by inject({}) do |groups, element| (groups[yield(element)] ||= []) << element groups end end |