Module: Denumerable
- Included in:
- Denumerator
- Defined in:
- lib/core/facets/denumerable.rb
Overview
Classes which include Denumerable will get versions of map, select, and so on, which return a Denumerator, so that they work horizontally without creating intermediate arrays.
Instance Method Summary collapse
- #map ⇒ Object (also: #collect)
- #reject ⇒ Object
- #select ⇒ Object (also: #find_all)
-
#skip(n) ⇒ Object
Skip the first n items in the list.
-
#take(n) ⇒ Object
Limit to the first n items in the list.
Instance Method Details
#map ⇒ Object Also known as: collect
11 12 13 14 15 16 17 |
# File 'lib/core/facets/denumerable.rb', line 11 def map Denumerator.new do |output| each do |*input| output.yield yield(*input) end end end |
#reject ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/core/facets/denumerable.rb', line 31 def reject Denumerator.new do |output| each do |*input| output.yield(*input) unless yield(*input) end end end |
#select ⇒ Object Also known as: find_all
21 22 23 24 25 26 27 |
# File 'lib/core/facets/denumerable.rb', line 21 def select Denumerator.new do |output| each do |*input| output.yield(*input) if yield(*input) end end end |
#skip(n) ⇒ Object
Skip the first n items in the list
52 53 54 55 56 57 58 59 60 |
# File 'lib/core/facets/denumerable.rb', line 52 def skip(n) Denumerator.new do |output| count = 0 each do |*input| output.yield(*input) if count >= n count += 1 end end end |
#take(n) ⇒ Object
Limit to the first n items in the list
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/core/facets/denumerable.rb', line 40 def take(n) Denumerator.new do |output| count = 0 each do |*input| break if count >= n output.yield(*input) count += 1 end end end |