Skip to content

Instantly share code, notes, and snippets.

@lucacervasio
Created July 23, 2013 16:05
Show Gist options
  • Save lucacervasio/6063648 to your computer and use it in GitHub Desktop.
Save lucacervasio/6063648 to your computer and use it in GitHub Desktop.
tree from dotted separated strings
#!/usr/bin/env ruby
a = {
'uno.due.tre.otto' => 2,
'uno.due.tre.sei' => 5,
'uno.due.quattro' => 'ciao',
'uno.sette.cinque' => 'true',
}
mib = {'uno' => {}}
a.each_pair do |item,val|
cur = mib
item.split(".").each_with_index do |part,index|
if cur.has_key?(part)
cur = cur[part]
else
if item.split(".").length - 1 == index
cur[part] = val
else
cur[part] = {}
end
cur = cur[part]
end
end
end
p mib
puts mib['uno']['due']['tre']['otto']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment