Class: ParallelEnumerable

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

Instance Method Summary collapse

Constructor Details

#initialize(enumerable) ⇒ ParallelEnumerable

Returns a new instance of ParallelEnumerable.



11
12
13
# File 'lib/parallel_enumerable.rb', line 11

def initialize(enumerable)
  @enumerable = enumerable
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



39
40
41
42
43
44
# File 'lib/parallel_enumerable.rb', line 39

def method_missing(method_name, *args, &block)
  return super unless enumerable.respond_to?(method_name)

  $stderr.puts "[parallel-enumerable] ##{method_name} is not defined"
  enumerable.public_send(method_name, *args, &block)
end

Instance Method Details

#eachObject



17
18
19
20
21
22
23
# File 'lib/parallel_enumerable.rb', line 17

def each
  enumerable.map do |item|
    Houston.async! do
      yield item
    end
  end.each(&:join)
end

#mapObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/parallel_enumerable.rb', line 25

def map
  queue = Queue.new

  each do |item|
    queue << yield(item)
  end

  [].tap do |results|
    results.push queue.pop until queue.empty?
  end
end