From: Mike Moore Date: 2011-03-05T03:43:00+09:00 Subject: Re: How to assign an element to a hash only if its value is not nil? On Fri, Mar 4, 2011 at 10:56 AM, Colin Bartlett wrote: > > I'm not sure what that code does, but taking what the OP wanted to do > as the starting point. >> hash = {} >>    hash[:key] = myvalue >> How to assign hash[:key] only if myvalue is not nil? >> Current solution is: >>    hash[:key] = myvalue unless myvalue.nil? >> Is there something more beautiful? My assumption from the OP's question was that he wanted to populate a new hash so that the keys's contain non-nil values. I offered a slightly different approach to achieve the same result. (Please note I said you "could", not "should".) My apologies for inflicting ugly or dangerous code on you, I assumed the circumstances and tradeoffs necessary for using (or not using) such an approach would be obvious. My bad.