Idiomatic way to assign if not nil?

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 :wink:

Alfred

···

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?

Neat - you did that with superators? Or some other way?

Jay

···

On Thu, 30 Aug 2007 23:40:39 -0700, aalfred wrote:

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

How did you do that, given that those operators are (as you say) non-
existing, and the code in your resulting code produces a syntax error
(under 1.8.6, anyhow)? Did you modify the Ruby source?

···

On Aug 31, 12:40 am, aalfred <alfred.anzlo...@gmail.com> wrote:

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

> 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

How did you do that, given that those operators are (as you say) non-
existing, and the code in your resulting code produces a syntax error
(under 1.8.6, anyhow)? Did you modify the Ruby source?

I don't think he did any such thing. I think when he says he rewrote
it, he means just that, he rewrote it, not that it actually worked or
anything :slight_smile:

···

On 9/2/07, Phrogz <phrogz@mac.com> wrote:

On Aug 31, 12:40 am, aalfred <alfred.anzlo...@gmail.com> wrote:

No, I did not modify ruby source (for "it is not the right thing" :).

What I did was just cleaning (DRYing) up the code. And as Logan
correctly
observed came to that 'code'. Since it cannot be done in the way I
wrote
it by adding two methods to the foo class which did the trick.

But it looks like others are bumping into similar/same repeating
patterns,
so something like that would be nice to have :wink:

Alfred

···

On Sep 2, 5:00 pm, "Logan Capaldo" <logancapa...@gmail.com> wrote:

> How did you do that, given that those operators are (as you say) non-
> existing, and the code in your resulting code produces a syntax error
> (under 1.8.6, anyhow)? Did you modify the Ruby source?

I don't think he did any such thing. I think when he says he rewrote
it, he means just that, he rewrote it, not that it actually worked or
anything :slight_smile: