Sha256: bf0f9321eb384f13f704eb86d47c0c0a56debda89fd0e40d1e134dc309d2aac9

Contents?: true

Size: 271 Bytes

Versions: 10

Compression:

Stored size: 271 Bytes

Contents

module Enumerable

  # 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 = {}; inject([]) {|a,x| h[yield(x)] ||= a << x}
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/enumerable/uniq_by.rb
facets-2.8.3 lib/core/facets/enumerable/uniq_by.rb
facets-2.8.2 lib/core/facets/enumerable/uniq_by.rb
facets-2.8.1 lib/core/facets/enumerable/uniq_by.rb
facets-2.8.0 lib/core/facets/enumerable/uniq_by.rb
facets-2.7.0 lib/core/facets/enumerable/uniq_by.rb
facets-2.6.0 lib/core/facets/enumerable/uniq_by.rb
facets-2.5.0 lib/core/facets/enumerable/uniq_by.rb
facets-2.5.1 lib/core/facets/enumerable/uniq_by.rb
facets-2.5.2 lib/core/facets/enumerable/uniq_by.rb