Sha256: ba60ac09b3c9669423ad3a6b82dfd5489aca05769675e0e8cff29ea0fda92f03

Contents?: true

Size: 334 Bytes

Versions: 7

Compression:

Stored size: 334 Bytes

Contents

class Array

  # Like #uniq, but determines uniqueness based on a given block.
  #
  #   a = (-5..5).to_a
  #   a.uniq_by!{ |i| i*i }
  #   a #=> [-5, -4, -3, -2, -1, 0]
  #
  # As can be seen in the example, order is significant.
  def uniq_by! #:yield:
    h = {}
    replace( inject([]){|a,x| h[yield(x)] ||= a << x} )
  end

end

Version data entries

7 entries across 6 versions & 1 rubygems

Version Path
facets-2.9.3 lib/core/facets/array/uniq_by.rb
facets-2.9.2 src/core/facets/array/uniq_by.rb
facets-2.9.2 lib/core/facets/array/uniq_by.rb
facets-2.9.1 lib/core/facets/array/uniq_by.rb
facets-2.9.0 lib/core/facets/array/uniq_by.rb
facets-2.9.0.pre.2 lib/core/facets/array/uniq_by.rb
facets-2.9.0.pre.1 lib/core/facets/array/uniq_by.rb