Clean way to assign value unless empty

Posted by atmorell on Stack Overflow See other posts from Stack Overflow or by atmorell
Published on 2010-06-03T21:25:51Z Indexed on 2010/06/03 21:34 UTC
Read the original article Hit count: 228

Filed under:

Hello,

I often need to assign a variable, if the source value is set. So far I have done it like this:

filters[:red] = params[:search][:red] unless params[:search][:red].nil?

This works but looks a bit clumsy. There must be a more DRY way of getting this result.

Any suggestions?

Best regards. Asbjørn Morell.

© Stack Overflow or respective owner

Related posts about ruby