From: Xavier Noria Date: 2011-03-25T03:53:36+09:00 Subject: Re: shortcut for add unless nil ? You could do something like this (untested): [a, b, c, d].compact.reduce(:+) though whether it is more clear is debatable. I think it reads fine if you're fluent. Sent from my iPhone El 24/03/2011, a les 19:36, Iain Barnett va escriure: > Hi, > > Is was wondering if there's a Ruby idiom for adding variables that should be arrays but that may be nil, for example: > > [1] + (a || [ ]) + (b || [ ]) + (c || [ ]) > > but that's a bit ugly IMO. I've had a look, but didn't see anything. I know I could use a fold or something similar, but I was wondering if there was something in the realms of ||= (i.e. nice, idiomatic shortcut) for this kind of thing? > > > Iain