Guest User

Untitled

a guest
Nov 23rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.36 KB | None | 0 0
  1. # Initialize missing values for subhashes
  2. h = Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) } #=> {}
  3. h['a']['b']['c'] = 'd'
  4. h #=> {"a"=>{"b"=>{"c"=>"d"}}}
  5.  
  6. # Initialize values for hash, for specific given keys
  7. list = %w[ a b c ]
  8. hash = Hash[list.collect { |i| [ i, 0 ] }]
  9.  
  10. # Initialize hash with default value of 0 for any given key
  11. hash = Hash.new { |h, k| h[k] = 0 }
Add Comment
Please, Sign In to add comment