Method: Array#uniq_by!
- Defined in:
- lib/core/facets/array/uniq_by.rb
#uniq_by! ⇒ Object
Like #uniq, but determines uniqueness based on a given block.
a = (-5..5).to_a
a.uniq_by!{ |i| i*i }
a #=> [-5, -4, -3, -2, -1, 0]
As can be seen in the example, order is significant.
10 11 12 13 |
# File 'lib/core/facets/array/uniq_by.rb', line 10 def uniq_by! #:yield: h = {} replace( inject([]){|a,x| h[yield(x)] ||= a << x} ) end |