Module: Enumerable

Defined in:
lib/casual_support/enumerable/index_to.rb,
lib/casual_support/enumerable/duplicates.rb

Instance Method Summary collapse

Instance Method Details

#duplicatesEnumerable

Returns the first duplicate of each element, preserving order of appearance.

Examples:

%w[a a b c c b a d].duplicates  # == ["a", "c", "b"]

Returns:



10
11
12
13
# File 'lib/casual_support/enumerable/duplicates.rb', line 10

def duplicates
  seen = Hash.new(0)
  self.select{|element| (seen[element] += 1) == 2 }
end

#index_to {|element| ... } ⇒ Hash{'E => 'V}

Converts the Enumerable into a Hash, using its elements as keys and using the given block to compute an associated value for each key.

Examples:

cache = id_list.index_to{|id| find_by_id(id) }

Yield Parameters:

  • element ('E)

    element from the Enumerable

Yield Returns:

  • ('V)

    value to associate with the element key

Returns:



14
15
16
# File 'lib/casual_support/enumerable/index_to.rb', line 14

def index_to()
  self.reduce({}){|h, k| h.put!(k, (yield k)) }
end