Last active
August 29, 2015 14:04
-
-
Save sschimansky/15aa276001ed44a73f7b to your computer and use it in GitHub Desktop.
Hash#deep_underscore
This file contains hidden or 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 | |
| def deep_underscore(h = nil) | |
| nh = {} | |
| (h || self).each do |k,v| | |
| nk = k.underscore | |
| if v.is_a? Hash | |
| puts "key #{k} recusive" | |
| nh[nk] = deep_underscore v | |
| elsif v.is_a? Array | |
| nh[nk] = v.map do |eh| | |
| deep_underscore eh | |
| end | |
| else | |
| puts "key #{k} no - hash" | |
| nh[nk] = v | |
| end | |
| h.delete v | |
| end | |
| nh | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment