Created
October 27, 2011 14:13
-
-
Save vidmantas/1319650 to your computer and use it in GitHub Desktop.
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
def transition_hash(h) | |
def clean_keys(h) | |
nh = {} | |
h.each_pair do |k,v| | |
if v.is_a?(Array) and v.first.is_a?(String) | |
nh[k.to_sym] = v.first.strip | |
elsif v.is_a?(Array) | |
nh[k.to_sym] = [] | |
v.each{|i| nh[k.to_sym] << clean_keys(i) } | |
end | |
end | |
nh | |
end | |
# get one level down | |
h.each_pair do |key, value| | |
if value.is_a?(Array) and value.first.is_a?(Hash) and value.first.keys.size == 1 | |
value = value.first[value.first.keys.first] | |
h[key] = value | |
end | |
end | |
clean_keys(h) | |
end | |
hash = transition_hash(hash) | |
pp hash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment