Created
September 12, 2013 12:39
-
-
Save kelmandor/6536658 to your computer and use it in GitHub Desktop.
instantiate array/hash if it doesnt exist to instert a value. this is to avoid exceptions (done in a sexy way)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| irb(main):001:0> a = {} | |
| => {} | |
| irb(main):002:0> a[:b][:c] = 3 | |
| NoMethodError: undefined method `[]=' for nil:NilClass | |
| from (irb):2 | |
| from /Users/dkelman/.rbenv/versions/1.9.3-p327/bin/irb:12:in `<main>' | |
| ========================================= | |
| a = {} | |
| (a[:b] ||= {})[:c] = 3 | |
| ============================== | |
| (hsh[line[:date]] ||=[]) << "blahblah" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment