Module: Enumerable
- Included in:
- StrokeDB::InvertedList, StrokeDB::SimpleSkiplist, StrokeDB::Skiplist, StrokeDB::Store, StrokeDB::Validations::InstanceMethods::Errors
- Defined in:
- lib/strokedb/core_ext/enumerable.rb
Overview
extracted from ActiveRecord (rubyforge.org/projects/activesupport/)
Instance Method Summary collapse
- #each_consecutive_pair ⇒ Object
-
#group_by ⇒ Object
Collect an enumerable into sets, grouped by the result of a block.
-
#map_with_index ⇒ Object
(also: #collect_with_index)
Map and each_with_index combined.
Instance Method Details
#each_consecutive_pair ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/strokedb/core_ext/enumerable.rb', line 22 def each_consecutive_pair first = true prev = nil each do |val| unless first yield prev, val else first = false end prev = val end end |
#group_by ⇒ Object
Collect an enumerable into sets, grouped by the result of a block. Useful, for example, for grouping records by date.
6 7 8 9 10 11 |
# File 'lib/strokedb/core_ext/enumerable.rb', line 6 def group_by inject({}) do |groups, element| (groups[yield(element)] ||= []) << element groups end end |
#map_with_index ⇒ Object Also known as: collect_with_index
Map and each_with_index combined.
14 15 16 17 18 |
# File 'lib/strokedb/core_ext/enumerable.rb', line 14 def map_with_index collected=[] each_with_index {|item, index| collected << yield(item, index) } collected end |