Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 24th, 2012  |  syntax: None  |  size: 0.52 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Rails source code : initialize hash in a weird way?
  2. @load_hooks = Hash.new {|h,k| h[k] = [] }
  3.        
  4. @load_hooks = Hash.new
  5.        
  6. # While this creates a new default object each time
  7. h = Hash.new { |hash, key| hash[key] = "Go Fish: #{key}" }
  8. h["c"]           #=> "Go Fish: c"
  9. h["c"].upcase!   #=> "GO FISH: C"
  10. h["d"]           #=> "Go Fish: d"
  11. h.keys           #=> ["c", "d"]
  12.        
  13. irb(main):001:0> a = Hash.new {|h,k| h[k] = [] }
  14. => {}
  15. irb(main):002:0> b = Hash.new
  16. => {}
  17. irb(main):003:0> a[123]
  18. => []
  19. irb(main):004:0> b[123]
  20. => nil