Skip to content

Instantly share code, notes, and snippets.

@kelmandor
Created September 12, 2013 12:39
Show Gist options
  • Select an option

  • Save kelmandor/6536658 to your computer and use it in GitHub Desktop.

Select an option

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)
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