Class: EnumeratorConcurrent::Queued
- Defined in:
- lib/enumerator_concurrent/queued.rb
Overview
Enumerator with thread count speciyfied to handle loop
Instance Method Summary collapse
- #each {|x| ... } ⇒ self
-
#initialize(arr, thread_count) ⇒ Queued
constructor
A new instance of Queued.
-
#map {|x| ... } ⇒ Array
With modified values.
Methods inherited from Array
Constructor Details
#initialize(arr, thread_count) ⇒ Queued
Returns a new instance of Queued.
7 8 9 10 11 12 |
# File 'lib/enumerator_concurrent/queued.rb', line 7 def initialize(arr, thread_count) @thread_count = 0...thread_count @queue = Queue.new @arr = arr super(arr) end |
Instance Method Details
#each {|x| ... } ⇒ self
16 17 18 19 20 21 22 23 24 |
# File 'lib/enumerator_concurrent/queued.rb', line 16 def each @arr.each { |x| @queue.push x } init_workers do pop_queue do |x| yield(x) end end self end |
#map {|x| ... } ⇒ Array
Returns with modified values.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/enumerator_concurrent/queued.rb', line 28 def map return_value = [] @arr.map.with_index { |x, i| @queue.push(i => x) } init_workers do pop_queue do |x| key, value = x.first return_value[key] = yield(value) end end return_value end |