Module: Twitter::Utils

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.flat_pmap(enumerable) ⇒ 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.



23
24
25
26
# File 'lib/twitter/utils.rb', line 23

def flat_pmap(enumerable)
  return to_enum(:flat_pmap, enumerable) unless block_given?
  pmap(enumerable, &Proc.new).flatten!(1)
end

.pmap(enumerable) ⇒ 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.



34
35
36
37
38
39
40
41
# File 'lib/twitter/utils.rb', line 34

def pmap(enumerable)
  return to_enum(:pmap, enumerable) unless block_given?
  if enumerable.count == 1
    enumerable.collect { |object| yield(object) }
  else
    enumerable.collect { |object| Thread.new { yield(object) } }.collect(&:value)
  end
end