Module: Enumerable
- Defined in:
- lib/casual_support/enumerable/index_to.rb,
lib/casual_support/enumerable/duplicates.rb
Instance Method Summary collapse
-
#duplicates ⇒ Enumerable
Returns the first-appearing duplicate of each element, preserving order of appearance.
-
#index_to {|key| ... } ⇒ Hash{key => value}
Creates a Hash using the Enumerable’s elements as keys, and using the given block to compute an associated value for each key.
Instance Method Details
#duplicates ⇒ Enumerable
Returns the first-appearing duplicate of each element, preserving order of appearance.
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 {|key| ... } ⇒ Hash{key => value}
Creates a Hash using the Enumerable’s elements as keys, and using the given block to compute an associated value for each key.
15 16 17 |
# File 'lib/casual_support/enumerable/index_to.rb', line 15 def index_to() self.reduce({}){|h, k| h.put!(k, (yield k)) } end |