Sha256: 86fd6d983703430e4b1c39c0bacc4b0a55b65e226b482898030670d6c945bb97

Contents?: true

Size: 288 Bytes

Versions: 7

Compression:

Stored size: 288 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]
  #
  #   CREDIT: ?

  def uniq_by #:yield:
    h = {}; inject([]) {|a,x| h[yield(x)] ||= a << x}
  end

end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
facets-2.4.0 lib/facets/enumerable/uniq_by.rb
facets-2.4.1 lib/facets/enumerable/uniq_by.rb
facets-2.4.2 lib/core/facets/enumerable/uniq_by.rb
facets-2.4.3 lib/core/facets/enumerable/uniq_by.rb
facets-2.4.4 lib/core/facets/enumerable/uniq_by.rb
facets-2.4.5 lib/core/facets/enumerable/uniq_by.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/enumerable/uniq_by.rb