From: Ryan Davis Date: 2011-03-25T07:45:27+09:00 Subject: Re: shortcut for add unless nil ? On Mar 24, 2011, at 11:36 , Iain Barnett wrote: > 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? The most idiomatic (and performant) shortcut is to not have the situation arise in the first place. Nil checks everywhere is generally a design smell. Push things up to whereever the defaults should be figured out... in railsy parlance: thingies = params[:thingies] || [] then later you never have to worry about it.