From 800d5ae36c8a906e6fb26d4064a6a75236f84186 Mon Sep 17 00:00:00 2001 From: Bernard Potocki Date: Tue, 12 May 2015 17:48:09 +0200 Subject: ActiveSupport::HashWithIndifferentAccess select and reject should return enumerator if called without block --- activesupport/CHANGELOG.md | 7 +++++++ .../lib/active_support/hash_with_indifferent_access.rb | 2 ++ activesupport/test/core_ext/hash_ext_test.rb | 10 ++++++++++ 3 files changed, 19 insertions(+) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 17c2f0eb27..1c69e0eb12 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,10 @@ +* ActiveSupport::HashWithIndifferentAccess `select` and `reject` will now return + enumerator if called without block. + + Fixes #20095 + + *Bernard Potocki* + * Removed `ActiveSupport::Concurrency::Latch`, superseded by `Concurrent::CountDownLatch` from the concurrent-ruby gem. diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 63690a1342..c5d35b84f0 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -238,10 +238,12 @@ module ActiveSupport def to_options!; self end def select(*args, &block) + return to_enum(:select) unless block_given? dup.tap { |hash| hash.select!(*args, &block) } end def reject(*args, &block) + return to_enum(:reject) unless block_given? dup.tap { |hash| hash.reject!(*args, &block) } end diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 663f782611..4624338df1 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -551,6 +551,11 @@ class HashExtTest < ActiveSupport::TestCase assert_instance_of ActiveSupport::HashWithIndifferentAccess, hash end + def test_indifferent_select_returns_enumerator + enum = ActiveSupport::HashWithIndifferentAccess.new(@strings).select + assert_instance_of Enumerator, enum + end + def test_indifferent_select_returns_a_hash_when_unchanged hash = ActiveSupport::HashWithIndifferentAccess.new(@strings).select {|k,v| true} @@ -572,6 +577,11 @@ class HashExtTest < ActiveSupport::TestCase assert_instance_of ActiveSupport::HashWithIndifferentAccess, hash end + def test_indifferent_reject_returns_enumerator + enum = ActiveSupport::HashWithIndifferentAccess.new(@strings).reject + assert_instance_of Enumerator, enum + end + def test_indifferent_reject_bang indifferent_strings = ActiveSupport::HashWithIndifferentAccess.new(@strings) indifferent_strings.reject! {|k,v| v != 1} -- cgit v1.2.3