Sha256: b2aa9ab2db8984cb6d9cd5bf7554c80010a6c590b7bcfd8a3cdf36dabe55e184

Contents?: true

Size: 352 Bytes

Versions: 12

Compression:

Stored size: 352 Bytes

Contents

class Array
  # Return an unique array based on the criteria given as a proc.
  #
  #   [1, 2, 3, 4].uniq_by { |i| i.odd? }
  #   #=> [1, 2]
  #
  def uniq_by
    hash, array = {}, []
    each { |i| hash[yield(i)] ||= (array << i) }
    array
  end

  # Same as uniq_by, but modifies self.
  def uniq_by!
    replace(uniq_by{ |i| yield(i) })
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
activesupport-3.0.0.rc lib/active_support/core_ext/array/uniq_by.rb
csd-0.1.5 lib/active_support/core_ext/array/uniq_by.rb
csd-0.1.4 lib/active_support/core_ext/array/uniq_by.rb
csd-0.1.3 lib/active_support/core_ext/array/uniq_by.rb
csd-0.1.2 lib/active_support/core_ext/array/uniq_by.rb
csd-0.1.1 lib/active_support/core_ext/array/uniq_by.rb
csd-0.1.0 lib/active_support/core_ext/array/uniq_by.rb
csd-0.0.16 lib/active_support/core_ext/array/uniq_by.rb
activesupport-3.0.0.beta4 lib/active_support/core_ext/array/uniq_by.rb
activesupport-3.0.0.beta3 lib/active_support/core_ext/array/uniq_by.rb
activesupport-3.0.0.beta2 lib/active_support/core_ext/array/uniq_by.rb
activesupport-3.0.0.beta lib/active_support/core_ext/array/uniq_by.rb