From: "David J. Hamilton" Date: 2011-03-04T02:16:38+09:00 Subject: Re: How to assign an element to a hash only if its value is not nil? Excerpts from Thomas W.'s message of Thu Mar 03 09:10:26 -0800 2011: > 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? There is an alternate way. Whether it is more beautiful or not is for you to decide. myvalue and hash[:hey] = myvalue ruby-1.9.2-p136 :001 > h, m, n = {}, 3, nil => [{}, 3, nil] ruby-1.9.2-p136 :003 > m and h[:m] = m => 3 ruby-1.9.2-p136 :004 > n and h[:n] = n => nil ruby-1.9.2-p136 :005 > h => {:m=>3} -- med v辰nlig h辰lsning David J. Hamilton