From: Michael Edgar Date: 2011-03-04T02:38:05+09:00 Subject: Re: How to assign an element to a hash only if its value is not nil? If you don't mind also overwriting the value if the value is false as well as if the value is nil, you can use the common ||= approach: hash[:key] ||= myvalue Michael Edgar adgar@carboni.ca http://carboni.ca/ On Mar 3, 2011, at 12:10 PM, Thomas W. wrote: > 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? > > -- > Posted via http://www.ruby-forum.com/. >