On Aug 29, 1:25 am, Jay Levitt <j... / jay.fm> wrote:
> I know I can use "a ||= b" to assign b to a if a is nil.
>
> But what about an equivalent to
>
> a = b if b
>
> Does that exist in a DRYer form?

Recently I had a similar problem. After some refactoring of code
I rewrote the code with two (non-existing, but very DRY operators)

I had those two cases:

  foo = bar if !bar.nil?                 # (case 1)  or
  foo = bar if foo.nil? && !bar.nil?     # (case 2)

and rewrote them into:

  foo ??= bar       # case 1
  foo !!= bar       # case 2

While this is not the same as your problem, it is quite similar.
I looks as this is not a completely isolated problem. Probably
because so many want to DRY their code. It looks like baroqueness
of code can survive only in the names of methods and variables ;-)

Alfred