Sha256: 36fb3853e45332a009477cbc4b5a53cad9aa824f68b99d91934a1d803f2fa8b1

Contents?: true

Size: 254 Bytes

Versions: 10

Compression:

Stored size: 254 Bytes

Contents

module Enumerable

  # Like #uniq, but determines uniqueness based on a given block.
  #
  #   (-5..5).to_a.uniq_by {|i| i*i }
  #   #=> [-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 9 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/enumerable/uniq_by.rb
facets-3.1.0 lib/core/facets/enumerable/uniq_by.rb
facets-3.0.0 lib/core/facets/enumerable/uniq_by.rb
facets-2.9.3 lib/core/facets/enumerable/uniq_by.rb
facets-2.9.2 src/core/facets/enumerable/uniq_by.rb
facets-2.9.2 lib/core/facets/enumerable/uniq_by.rb
facets-2.9.1 lib/core/facets/enumerable/uniq_by.rb
facets-2.9.0 lib/core/facets/enumerable/uniq_by.rb
facets-2.9.0.pre.2 lib/core/facets/enumerable/uniq_by.rb
facets-2.9.0.pre.1 lib/core/facets/enumerable/uniq_by.rb