Sha256: 78f91a6cdc6914d85009c5944ba656c07bb2e5db41e66f0bf92d4bab33c30cad

Contents?: true

Size: 311 Bytes

Versions: 3

Compression:

Stored size: 311 Bytes

Contents

require 'facets/enumerable/uniq_by'

class Array

  # Like #uniq, but determines uniqueness based on a given block.
  #
  #   (-5..5).to_a.uniq_by {|i| i*i }
  #
  # produces
  #
  #   [-5, -4, -3, -2, -1, 0]
  #
  def uniq_by! #:yield:
    h = {}; replace(inject([]){|a,x| h[yield(x)] ||= a << x})
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/array/uniq_by.rb
facets-2.8.3 lib/core/facets/array/uniq_by.rb
facets-2.8.2 lib/core/facets/array/uniq_by.rb