1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

deprecate Array#uniq_by and Array#uniq_by! in favor of Array#uniq and Array#uniq! from ruby 1.9

This commit is contained in:
Vasiliy Ermolovich 2011-12-22 19:30:10 +03:00
parent fa5adfb1e8
commit c4df2d0b6e
2 changed files with 22 additions and 12 deletions

View file

@ -3,14 +3,16 @@ class Array
#
# [1, 2, 3, 4].uniq_by { |i| i.odd? } # => [1, 2]
#
def uniq_by
hash, array = {}, []
each { |i| hash[yield(i)] ||= (array << i) }
array
def uniq_by(&block)
ActiveSupport::Deprecation.warn "uniq_by " \
"is deprecated. Use Array#uniq instead", caller
uniq(&block)
end
# Same as uniq_by, but modifies self.
def uniq_by!
replace(uniq_by{ |i| yield(i) })
def uniq_by!(&block)
ActiveSupport::Deprecation.warn "uniq_by! " \
"is deprecated. Use Array#uniq! instead", caller
uniq!(&block)
end
end

View file

@ -343,22 +343,30 @@ end
class ArrayUniqByTests < Test::Unit::TestCase
def test_uniq_by
assert_equal [1,2], [1,2,3,4].uniq_by { |i| i.odd? }
assert_equal [1,2], [1,2,3,4].uniq_by(&:even?)
assert_equal((-5..0).to_a, (-5..5).to_a.uniq_by{ |i| i**2 })
ActiveSupport::Deprecation.silence do
assert_equal [1,2], [1,2,3,4].uniq_by { |i| i.odd? }
assert_equal [1,2], [1,2,3,4].uniq_by(&:even?)
assert_equal((-5..0).to_a, (-5..5).to_a.uniq_by{ |i| i**2 })
end
end
def test_uniq_by!
a = [1,2,3,4]
a.uniq_by! { |i| i.odd? }
ActiveSupport::Deprecation.silence do
a.uniq_by! { |i| i.odd? }
end
assert_equal [1,2], a
a = [1,2,3,4]
a.uniq_by! { |i| i.even? }
ActiveSupport::Deprecation.silence do
a.uniq_by! { |i| i.even? }
end
assert_equal [1,2], a
a = (-5..5).to_a
a.uniq_by! { |i| i**2 }
ActiveSupport::Deprecation.silence do
a.uniq_by! { |i| i**2 }
end
assert_equal((-5..0).to_a, a)
end
end