Skip to content

Instantly share code, notes, and snippets.

@lmarburger
Created August 29, 2014 13:38
Show Gist options
  • Save lmarburger/e6d620d563f70f184b9b to your computer and use it in GitHub Desktop.
Save lmarburger/e6d620d563f70f184b9b to your computer and use it in GitHub Desktop.
Ruby's hash default value
$x = 0
h = Hash.new {|h, k| h[k] = $x += 1 }
p h[:a]
p h[:a]
p h[:b]
p h
$y = 0
i = Hash.new { $y += 1 }
p i[:a]
p i[:b]
p i[:a]
p i
j = Hash.new(0)
p j[:a]
p j
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment