Class: Parallel

Inherits:
Object
  • Object
show all
Defined in:
lib/parallel2.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, &block) ⇒ Parallel

Returns a new instance of Parallel.



17
18
19
20
# File 'lib/parallel2.rb', line 17

def initialize(collection, &block)
  @collection   = collection
  @block        = block
end

Class Method Details

.each(collection, &block) ⇒ Object



6
7
8
9
10
# File 'lib/parallel2.rb', line 6

def self.each(collection, &block)
  klass = new(collection, &block)
  klass.parallelized_each
  collection
end

.map(collection, &block) ⇒ Object



12
13
14
15
# File 'lib/parallel2.rb', line 12

def self.map(collection, &block)
  klass = new(collection, &block)
  klass.parallelized_map
end

Instance Method Details

#futuresObject



30
31
32
33
34
# File 'lib/parallel2.rb', line 30

def futures
  @futures ||= @collection.map do |item|
    Celluloid::Future.new(item, &@block)
  end
end

#parallelized_eachObject



22
23
24
# File 'lib/parallel2.rb', line 22

def parallelized_each
  futures.each(&:value)
end

#parallelized_mapObject



26
27
28
# File 'lib/parallel2.rb', line 26

def parallelized_map
  futures.map(&:value)
end