Skip to content

Instantly share code, notes, and snippets.

@jphenow
Created November 15, 2013 22:18
Show Gist options
  • Save jphenow/7492604 to your computer and use it in GitHub Desktop.
Save jphenow/7492604 to your computer and use it in GitHub Desktop.
WTF default value hash
2.0.0p247 :021 > h = Hash.new([])
# => {}
2.0.0p247 :022 > h[:test]
# => []
2.0.0p247 :023 > h[:test] << "h"
# => ["h"]
2.0.0p247 :024 > h
# => {}
2.0.0p247 :025 > h.values
# => []
2.0.0p247 :026 > h.keys
# => []
2.0.0p247 :027 > h[:test]
# => ["h"]
2.0.0p247 :028 > h[:something_else]
# => ["h"]
# So the shoveling of the "h" is modifying the default value (array on line 1) in place.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment