Module: Twitter::Utils

Class Method Summary collapse

Class Method Details

.flat_pmap(enumerable, &block) ⇒ Array, Enumerator

Returns a new array with the concatenated results of running block once for every element in enumerable. If no block is given, an enumerator is returned instead.

Parameters:

Returns:

  • (Array, Enumerator)


10
11
12
13
14
# File 'lib/twitter/utils.rb', line 10

def flat_pmap(enumerable, &block)
  return to_enum(:flat_pmap, enumerable) unless block

  pmap(enumerable, &block).flatten(1)
end

.pmap(enumerable, &block) ⇒ Array, Enumerator

Returns a new array with the results of running block once for every element in enumerable. If no block is given, an enumerator is returned instead.

Parameters:

Returns:

  • (Array, Enumerator)


21
22
23
24
25
26
27
28
29
# File 'lib/twitter/utils.rb', line 21

def pmap(enumerable, &block)
  return to_enum(:pmap, enumerable) unless block

  if enumerable.count == 1
    enumerable.collect(&block)
  else
    enumerable.collect { |object| Thread.new { yield(object) } }.collect(&:value)
  end
end