Skip to content

Instantly share code, notes, and snippets.

@marcioj
Created January 18, 2015 01:26
Show Gist options
  • Select an option

  • Save marcioj/2b8575174efbf4bd9842 to your computer and use it in GitHub Desktop.

Select an option

Save marcioj/2b8575174efbf4bd9842 to your computer and use it in GitHub Desktop.
Recursive hash creation in ruby
h = Hash.new {|h,k| h[k] = Hash.new(&h.default_proc) } # => {}
h[:a] = 1 # => 1
h[:b][:c] = 2 # => 2
h[:c][:d] = 3 # => 3
h # => {:a=>1, :b=>{:c=>2}, :c=>{:d=>3}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment