Create or add to an array in Ruby

foo ||= [] foo << :element 

Feels a little awkward. Is there a more idiomatic way?

+58
arrays ruby
Aug 28 2018-12-18T00:
source share
2 answers
 (foo ||= []) << :element 

But meh. Is it really so burdensome to be readable?

+99
Aug 28 2018-12-12T00:
source share

You can always use the push method for any array. I like it.

 (a ||= []).push(:element) 
+47
Aug 28 2018-12-12T00:
source share



All Articles