Skip to content

Instantly share code, notes, and snippets.

@vidmantas
Created October 27, 2011 14:13
Show Gist options
  • Save vidmantas/1319650 to your computer and use it in GitHub Desktop.
Save vidmantas/1319650 to your computer and use it in GitHub Desktop.
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