Create nested hashes


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # From: http://www.ruby-forum.com/topic/111524
  2. # Author: Daniel Martin
  3.  
  4. a = Hash.new{|h,k| h[k]=Hash.new(&h.default_proc) }
  5.  
  6. a[2][1]=2
  7. a[2][2][3]=4
  8. a[3][1][1][1]=1
  9.  
  10. p a #=> {2=>{1=>2, 2=>{3=>4}}, 3=>{1=>{1=>{1=>1}}}}

URL: http://www.ruby-forum.com/topic/111524

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.