Module: Enumerable

Defined in:
lib/jetpants/monkeypatch.rb

Overview

Reopen Enumerable to add some concurrent iterators

Instance Method Summary collapse

Instance Method Details

#concurrent_eachObject

Works like each but runs the block in a separate thread per item.



6
7
8
9
# File 'lib/jetpants/monkeypatch.rb', line 6

def concurrent_each
  collect {|*item| Thread.new {yield *item}}.each {|th| th.join}
  self
end

#concurrent_each_with_index(&block) ⇒ Object

Works like each_with_index but runs the block in a separate thread per item.



17
18
19
# File 'lib/jetpants/monkeypatch.rb', line 17

def concurrent_each_with_index(&block)
  each_with_index.concurrent_each(&block)
end

#concurrent_mapObject

Works like map but runs the block in a separate thread per item.



12
13
14
# File 'lib/jetpants/monkeypatch.rb', line 12

def concurrent_map
  collect {|*item| Thread.new {yield *item}}.collect {|th| th.value}
end