Module: Twitter::Utils

Included in:
Base, Client, Cursor, GeoResults, REST::DirectMessages, REST::Favorites, REST::FriendsAndFollowers, REST::Lists, REST::SavedSearches, REST::Tweets, REST::Users, REST::Utils, SearchResults, TrendResults
Defined in:
lib/twitter/utils.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary (collapse)

Class Method Details

+ (Array, Enumerator) flat_pmap(enumerable, &block)

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)


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

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

+ (Array, Enumerator) pmap(enumerable)

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)


34
35
36
37
# File 'lib/twitter/utils.rb', line 34

def pmap(enumerable)
  return to_enum(:pmap, enumerable) unless block_given?
  pmap_with_index(enumerable).sort_by { |_, index| index }.collect { |object, _| yield(object) }
end

+ (Array, Enumerator) pmap_with_index(enumerable)

Calls block with two arguments, the item and its index, for each item in enumerable. Given arguments are passed through to pmap. If no block is given, an enumerator is returned instead.

Parameters:

Returns:

  • (Array, Enumerator)


45
46
47
48
49
50
51
52
53
# File 'lib/twitter/utils.rb', line 45

def pmap_with_index(enumerable)
  return to_enum(:pmap_with_index, enumerable) unless block_given?
  # Don't bother spawning a new thread if there's only one item
  if enumerable.count == 1
    enumerable.collect { |object| yield(object, 0) }
  else
    enumerable.each_with_index.collect { |object, index| Thread.new { yield(object, index) } }.collect(&:value)
  end
end