Skip to content

Instantly share code, notes, and snippets.

@chris
Created September 20, 2016 19:08
Show Gist options
  • Save chris/b4138603a8fe17e073c6bc073eb17785 to your computer and use it in GitHub Desktop.
Save chris/b4138603a8fe17e073c6bc073eb17785 to your computer and use it in GitHub Desktop.
A "deep" version of Rails' Hash#transform_values
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
@halilim
Copy link

halilim commented Jul 19, 2018

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