From e167389e1f2fc577e0d3cb645804d012aee2e89d Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Sat, 12 Jul 2014 16:44:25 +0200 Subject: Don't construct a Proc if no block is given --- activesupport/lib/active_support/core_ext/hash/transform_values.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/hash/transform_values.rb b/activesupport/lib/active_support/core_ext/hash/transform_values.rb index 6ff7e91212..cebf9e815a 100644 --- a/activesupport/lib/active_support/core_ext/hash/transform_values.rb +++ b/activesupport/lib/active_support/core_ext/hash/transform_values.rb @@ -4,7 +4,7 @@ class Hash # # { a: 1, b: 2, c: 3 }.transform_values { |x| x * 2 } # # => { a: 2, b: 4, c: 6 } - def transform_values(&block) + def transform_values result = self.class.new each do |key, value| result[key] = yield(value) -- cgit v1.2.3 From f1bad130d0c9bd77c94e43b696adca56c46a66aa Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Sat, 12 Jul 2014 18:22:43 +0200 Subject: Return an Enumerator if no block is given --- activesupport/lib/active_support/core_ext/hash/keys.rb | 4 +++- activesupport/lib/active_support/core_ext/hash/transform_values.rb | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb index 8657f34be2..f4105f66b0 100644 --- a/activesupport/lib/active_support/core_ext/hash/keys.rb +++ b/activesupport/lib/active_support/core_ext/hash/keys.rb @@ -6,7 +6,8 @@ class Hash # hash.transform_keys{ |key| key.to_s.upcase } # # => {"NAME"=>"Rob", "AGE"=>"28"} def transform_keys - result = {} + return enum_for(:transform_keys) unless block_given? + result = self.class.new each_key do |key| result[yield(key)] = self[key] end @@ -16,6 +17,7 @@ class Hash # Destructively convert all keys using the block operations. # Same as transform_keys but modifies +self+. def transform_keys! + return enum_for(:transform_keys!) unless block_given? keys.each do |key| self[yield(key)] = delete(key) end diff --git a/activesupport/lib/active_support/core_ext/hash/transform_values.rb b/activesupport/lib/active_support/core_ext/hash/transform_values.rb index cebf9e815a..e9bcce761f 100644 --- a/activesupport/lib/active_support/core_ext/hash/transform_values.rb +++ b/activesupport/lib/active_support/core_ext/hash/transform_values.rb @@ -5,6 +5,7 @@ class Hash # { a: 1, b: 2, c: 3 }.transform_values { |x| x * 2 } # # => { a: 2, b: 4, c: 6 } def transform_values + return enum_for(:transform_values) unless block_given? result = self.class.new each do |key, value| result[key] = yield(value) @@ -14,6 +15,7 @@ class Hash # Destructive +transform_values+ def transform_values! + return enum_for(:transform_values!) unless block_given? each do |key, value| self[key] = yield(value) end -- cgit v1.2.3