# Strange syntax sugar

**URL:** <https://www.ruby-forum.com/t/strange-syntax-sugar/92776>\
**Category:** Ruby\
**Created:** [April 24, 2007, 2:41am UTC](https://www.ruby-forum.com/t/strange-syntax-sugar/92776 "2007-04-24T02:41:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aalfred](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@aalfred](https://www.ruby-forum.com/u/aalfred)\
**Post date:** [April 24, 2007, 2:41am UTC](https://www.ruby-forum.com/t/strange-syntax-sugar/92776/1 "2007-04-24T02:41:15Z")

</div>

I’ve recently had to deal with lots of assignments, where most of them  
looked  
like those two lines:

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

(I know it could be written using ‘unless’, but I strongly dislike  
‘unless’)

Then, while doing some refactoring of the code (and while searching  
for  
more idiomatic form to gain some more clarity in the code) I rewrote  
those as:

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

At the end it turned out there were only those two cases:

- case 1: ‘do-not-assign-nil’, and
- case 2: ‘do-not-assign-nil’ for ‘||=’ (similarity with !!=)

I thought I ‘discovered’ new fine operators! But I later found out  
that these  
cannot be new operators, but are just a possible syntatic sugar.

If you cannot understand those two lines quick, they are probably  
useless. And I  
admit that after rethinking they were not so simple to understand  
anymore, and  
they do not map clearly into: “foo = bar ‘stands for’ foo = foo  
 bar”.

But the code still looked ‘cute’ (or at least more ‘clean’) to me!

And so I decided to post it just as a question of what others think  
about it,  
about how often they would use it, and if anyone likes it at all.

This is not a proposal to sweeten the (already sweet) ruby syntax!

---

<div class="post-metadata">

**Author:** ![aalfred](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@aalfred](https://www.ruby-forum.com/u/aalfred)\
**Post date:** [April 24, 2007, 5:38am UTC](https://www.ruby-forum.com/t/strange-syntax-sugar/92776/2 "2007-04-24T05:38:25Z")

</div>

On Apr 23, 7:40 pm, aalfred [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve recently had to deal with lots of assignments, where most of them  
> looked  
> like those two lines:
> 
> foo = bar if !bar.nil? # (case 1) or  
> foo = bar if foo.nil? && !bar.nil? # (case 2)
> 
> (I know it could be written using ‘unless’, but I strongly dislike  
> ‘unless’)

I think you won’t need your new sugar if you just rewrite the  
originals:

foo = bar if bar # case 1  
foo ||= bar if bar # case 2

That’s the simplest and cleanest, I think.

Jeff

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![aalfred](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@aalfred](https://www.ruby-forum.com/u/aalfred)\
**Post date:** [April 24, 2007, 8:57am UTC](https://www.ruby-forum.com/t/strange-syntax-sugar/92776/3 "2007-04-24T08:57:37Z")

</div>

On 4/24/07, Jeff [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> [softiesonrails.com](http://softiesonrails.com)  
> Note thast when bar == false it won’t work.

Jano

---

<div class="post-metadata">

**Author:** ![aalfred](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@aalfred](https://www.ruby-forum.com/u/aalfred)\
**Post date:** [April 24, 2007, 9:36am UTC](https://www.ruby-forum.com/t/strange-syntax-sugar/92776/4 "2007-04-24T09:36:43Z")

</div>

On 24.04.2007 05:37, Jeff wrote:

> > 
> 
> I think you won’t need your new sugar if you just rewrite the  
> originals:
> 
> foo = bar if bar # case 1  
> foo ||= bar if bar # case 2
> 
> That’s the simplest and cleanest, I think.

That’s doubling the effort in the second case. Rather do

foo &&= bar  
foo ||= bar

As Jano pointed out, this reacts to false in the same way as to nil  
which might or might not be what you want.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![aalfred](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@aalfred](https://www.ruby-forum.com/u/aalfred)\
**Post date:** [April 24, 2007, 12:41pm UTC](https://www.ruby-forum.com/t/strange-syntax-sugar/92776/5 "2007-04-24T12:41:11Z")

</div>

On Apr 24, 5:37 am, Jeff [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ‘unless’)
> 
> I think you won’t need your new sugar if you just rewrite the  
> originals:
> 
> foo = bar if bar # case 1  
> foo ||= bar if bar # case 2
> 
> That’s the simplest and cleanest, I think.

There is a subtelty in the assignment that is hard to see at the first  
glance: you _never_ assign nil to foo (but assign can false).  
So your rewrite is not equivalent.

---

<div class="post-metadata">

**Author:** ![aalfred](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@aalfred](https://www.ruby-forum.com/u/aalfred)\
**Post date:** [April 26, 2007, 5:24pm UTC](https://www.ruby-forum.com/t/strange-syntax-sugar/92776/6 "2007-04-26T17:24:55Z")

</div>

Hi,

At Tue, 24 Apr 2007 19:40:06 +0900,  
aalfred wrote in [ruby-talk:248941]:

> > foo = bar if bar # case 1  
> > foo ||= bar if bar # case 2
> > 
> > That’s the simplest and cleanest, I think.
> 
> There is a subtelty in the assignment that is hard to see at the first  
> glance: you _never_ assign nil to foo (but assign can false).  
> So your rewrite is not equivalent.

Ruby doesn’t distinguish nil and false as boolean, so if ??=  
were introduced it would not assign false.
