diff options
author | José Valim <jose.valim@gmail.com> | 2011-12-22 09:22:32 -0800 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-12-22 09:22:32 -0800 |
commit | e003bafc116c8f8285653047388923500a2cc13f (patch) | |
tree | bb1cb572b6a600e35995619806829902b12f6781 /activesupport | |
parent | 6def76db7b92efc5e301503d542db0d62d3d8284 (diff) | |
parent | c4df2d0b6e0c1f4e0df4a66bfae91a684f3d9f71 (diff) | |
download | rails-e003bafc116c8f8285653047388923500a2cc13f.tar.gz rails-e003bafc116c8f8285653047388923500a2cc13f.tar.bz2 rails-e003bafc116c8f8285653047388923500a2cc13f.zip |
Merge pull request #4135 from nashby/deprecate-uniq-by
deprecate Array#uniq_by and Array#uniq_by!
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/array/uniq_by.rb | 14 | ||||
-rw-r--r-- | activesupport/test/core_ext/array_ext_test.rb | 20 |
2 files changed, 22 insertions, 12 deletions
diff --git a/activesupport/lib/active_support/core_ext/array/uniq_by.rb b/activesupport/lib/active_support/core_ext/array/uniq_by.rb index 9c5f97b0e9..edc164dabd 100644 --- a/activesupport/lib/active_support/core_ext/array/uniq_by.rb +++ b/activesupport/lib/active_support/core_ext/array/uniq_by.rb @@ -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 diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 6739b4afbd..278734027a 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -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 |