foo ||= []
foo << :element
Feels a little clunky. Is there a more idiomatic way?
(foo ||= []) << :element
But meh. Is it really so onerous to keep it readable?
foo itself is a more complex expression, like a (nested) hash in which you look up values, this is a nice way to avoid looking up the values multiple times or spending another variable for the looked up array value.You also could benefit from the Kernel#Array, like:
# foo = nil
foo = Array(foo).push(:element)
# => [:element]
which has the benefit of flattening a potential Array, like:
# foo = [1]
foo = Array(foo).push(:element)
# => [1, :element]
foo = foo sets foo to nil when foo is undefined. Also, Kernel#Array doesn't flatten foo. It just returns foo if it's an Array.
(foo ||= []) << :element, but I find it uglier.