fork download
  1. h = {}
  2. match = 'a.b.c'
  3. tokens = match.split('.')
  4. last = tokens[0..-2].inject(h) do |h, scope|
  5. h[scope] ||= {}
  6. end
  7. last[tokens.last] = 'untranslated'
  8. puts h.inspect
  9.  
  10. puts "----------"
  11.  
  12. def hash_by_recurse(matches, h)
  13. return h if matches.nil? || matches.size == 0
  14. h[matches.first] ||= {}
  15. hash_by_recurse(matches[1..-1], h[matches.first])
  16. h
  17. end
  18.  
  19. h = hash_by_recurse("a.b.c".split('.'), {})
  20. puts h.inspect
Success #stdin #stdout 0s 4760KB
stdin
Standard input is empty
stdout
{"a"=>{"b"=>{"c"=>"untranslated"}}}
----------
{"a"=>{"b"=>{"c"=>{}}}}