mirror of https://github.com/rails/rails
Merge pull request #21806 from kachick/fix-transformer-size
Return a sized Enumerator from Hash#transform_values{!}
This commit is contained in:
commit
24bddfc542
|
@ -10,7 +10,7 @@ class Hash
|
|||
#
|
||||
# hash.transform_keys.with_index { |k, i| [k, i].join } # => {"name0"=>"Rob", "age1"=>"28"}
|
||||
def transform_keys
|
||||
return enum_for(:transform_keys) unless block_given?
|
||||
return enum_for(:transform_keys) { size } unless block_given?
|
||||
result = self.class.new
|
||||
each_key do |key|
|
||||
result[yield(key)] = self[key]
|
||||
|
@ -21,7 +21,7 @@ class Hash
|
|||
# Destructively converts all keys using the +block+ operations.
|
||||
# Same as +transform_keys+ but modifies +self+.
|
||||
def transform_keys!
|
||||
return enum_for(:transform_keys!) unless block_given?
|
||||
return enum_for(:transform_keys!) { size } unless block_given?
|
||||
keys.each do |key|
|
||||
self[yield(key)] = delete(key)
|
||||
end
|
||||
|
|
|
@ -9,7 +9,7 @@ class Hash
|
|||
#
|
||||
# { a: 1, b: 2 }.transform_values.with_index { |v, i| [v, i].join.to_i } # => { a: 10, b: 21 }
|
||||
def transform_values
|
||||
return enum_for(:transform_values) unless block_given?
|
||||
return enum_for(:transform_values) { size } unless block_given?
|
||||
return {} if empty?
|
||||
result = self.class.new
|
||||
each do |key, value|
|
||||
|
@ -21,7 +21,7 @@ class Hash
|
|||
# Destructively converts all values using the +block+ operations.
|
||||
# Same as +transform_values+ but modifies +self+.
|
||||
def transform_values!
|
||||
return enum_for(:transform_values!) unless block_given?
|
||||
return enum_for(:transform_values!) { size } unless block_given?
|
||||
each do |key, value|
|
||||
self[key] = yield(value)
|
||||
end
|
||||
|
|
|
@ -18,15 +18,17 @@ class TransformKeysTest < ActiveSupport::TestCase
|
|||
assert_same original, mapped
|
||||
end
|
||||
|
||||
test "transform_keys returns an Enumerator if no block is given" do
|
||||
test "transform_keys returns a sized Enumerator if no block is given" do
|
||||
original = { a: 'a', b: 'b' }
|
||||
enumerator = original.transform_keys
|
||||
assert_equal original.size, enumerator.size
|
||||
assert_equal Enumerator, enumerator.class
|
||||
end
|
||||
|
||||
test "transform_keys! returns an Enumerator if no block is given" do
|
||||
test "transform_keys! returns a sized Enumerator if no block is given" do
|
||||
original = { a: 'a', b: 'b' }
|
||||
enumerator = original.transform_keys!
|
||||
assert_equal original.size, enumerator.size
|
||||
assert_equal Enumerator, enumerator.class
|
||||
end
|
||||
|
||||
|
|
|
@ -47,15 +47,17 @@ class TransformValuesTest < ActiveSupport::TestCase
|
|||
assert_nil mapped[:b]
|
||||
end
|
||||
|
||||
test "transform_values returns an Enumerator if no block is given" do
|
||||
test "transform_values returns a sized Enumerator if no block is given" do
|
||||
original = { a: 'a', b: 'b' }
|
||||
enumerator = original.transform_values
|
||||
assert_equal original.size, enumerator.size
|
||||
assert_equal Enumerator, enumerator.class
|
||||
end
|
||||
|
||||
test "transform_values! returns an Enumerator if no block is given" do
|
||||
test "transform_values! returns a sized Enumerator if no block is given" do
|
||||
original = { a: 'a', b: 'b' }
|
||||
enumerator = original.transform_values!
|
||||
assert_equal original.size, enumerator.size
|
||||
assert_equal Enumerator, enumerator.class
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue