Created
September 20, 2016 19:08
-
-
Save chris/b4138603a8fe17e073c6bc073eb17785 to your computer and use it in GitHub Desktop.
A "deep" version of Rails' Hash#transform_values
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
class Hash | |
class Hash | |
def deep_transform_values(&block) | |
reduce({}) do |result, (k, v)| | |
result[k] = v.is_a?(Hash) ? v.deep_transform_values(&block) : yield(v) | |
result | |
end | |
end | |
end | |
end |
To anyone else who reaches this page: v
can be an array.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hola, i have a question for you.
I've implemented it while ago as
i'm not sure what solution is clearer. why do you use reduce for that? Both solutions create new copy of hash..