Sha256: 18823da540d3f6c347a436ca7ff78df082a196ec14d926bc0ef269d57f107560
Contents?: true
Size: 730 Bytes
Versions: 2
Compression:
Stored size: 730 Bytes
Contents
module Enumerable # Returns the first duplicate of each element, preserving order of # appearance. If a block is given, it will use the return # value of the block for element comparison. # # Examples: # %w[a a b c d c b a].duplicates == %w[a c b] # %w[A a B b].duplicates(&:upcase) == %w[a b] # # @yield [elem] computes an indentifying value # @yieldparam elem element from the +Enumerable+ # @yieldreturn value used for duplicate comparison # @return [Enumerable] duplicate elements of the +Enumerable+ def duplicates seen = Hash.new(0) if block_given? self.select{|elem| (seen[yield(elem)] += 1) == 2 } else self.select{|elem| (seen[elem] += 1) == 2 } end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
casual_support-2.0.0 | lib/casual_support/enumerable/duplicates.rb |
casual_support-1.0.0 | lib/casual_support/enumerable/duplicates.rb |