Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Hash#map_values
to ActiveSupport
Didn't get a chance to convert existing code, I'll skim through the code base to make use of this later this afternoon.
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
activesupport/lib/active_support/core_ext/hash/transform_values.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Hash | ||
# Returns a new hash with the results of running +block+ once for every value. | ||
# The keys are unchanged. | ||
# | ||
# { a: 1, b: 2, c: 3 }.transform_values { |x| x * 2 } | ||
# # => { a: 2, b: 4, c: 6 } | ||
def transform_values(&block) | ||
result = self.class.new | ||
each do |key, value| | ||
result[key] = yield(value) | ||
end | ||
result | ||
end | ||
|
||
# Destructive +transform_values+ | ||
def transform_values! | ||
each do |key, value| | ||
self[key] = yield(value) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require 'abstract_unit' | ||
require 'active_support/core_ext/hash/indifferent_access' | ||
require 'active_support/core_ext/hash/transform_values' | ||
|
||
class TransformValuesTest < ActiveSupport::TestCase | ||
test "transform_values returns a new hash with the values computed from the block" do | ||
original = { a: 'a', b: 'b' } | ||
mapped = original.transform_values { |v| v + '!' } | ||
|
||
assert_equal({ a: 'a', b: 'b' }, original) | ||
assert_equal({ a: 'a!', b: 'b!' }, mapped) | ||
end | ||
|
||
test "transform_values! modifies the values of the original" do | ||
original = { a: 'a', b: 'b' } | ||
mapped = original.transform_values! { |v| v + '!' } | ||
|
||
assert_equal({ a: 'a!', b: 'b!' }, original) | ||
assert_same original, mapped | ||
end | ||
|
||
test "indifferent access is still indifferent after mapping values" do | ||
original = { a: 'a', b: 'b' }.with_indifferent_access | ||
mapped = original.transform_values { |v| v + '!' } | ||
|
||
assert_equal 'a!', mapped[:a] | ||
assert_equal 'a!', mapped['a'] | ||
end | ||
|
||
# This is to be consistent with the behavior of Ruby's built in methods | ||
# (e.g. #select, #reject) as of 2.2 | ||
test "default values do not persist during mapping" do | ||
original = Hash.new('foo') | ||
original[:a] = 'a' | ||
mapped = original.transform_values { |v| v + '!' } | ||
|
||
assert_equal 'a!', mapped[:a] | ||
assert_nil mapped[:b] | ||
end | ||
|
||
test "default procs do not persist after mapping" do | ||
original = Hash.new { 'foo' } | ||
original[:a] = 'a' | ||
mapped = original.transform_values { |v| v + '!' } | ||
|
||
assert_equal 'a!', mapped[:a] | ||
assert_nil mapped[:b] | ||
end | ||
end |