# Strange syntax sugar

**URL:** <https://rubytalk.org/t/strange-syntax-sugar/37142>\
**Category:** ruby-talk\
**Created:** [24 April 2007 00:40 UTC](https://rubytalk.org/t/strange-syntax-sugar/37142 "2007-04-24T00:40:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aalfred](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@aalfred](https://rubytalk.org/u/aalfred)\
**Post date:** [24 April 2007 00:40 UTC](https://rubytalk.org/t/strange-syntax-sugar/37142/1 "2007-04-24T00:40:06Z")

</div>

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

&nbsp;&nbsp;foo = bar if !bar.nil? # (case 1) or  
&nbsp;&nbsp;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:

&nbsp;&nbsp;foo ??= bar # case 1  
&nbsp;&nbsp;foo !!= bar # case 2

At the end it turned out there were only those two cases:  
&nbsp;&nbsp;- case 1: 'do-not-assign-nil', and  
&nbsp;&nbsp;- 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 \<op\>= bar 'stands for' foo = foo  
\<op\> 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!

> **···**
>
> --  
> Alfred

---

<div class="post-metadata">

**Author:** ![Jeff](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jeff](https://rubytalk.org/u/Jeff)\
**Post date:** [24 April 2007 03:37 UTC](https://rubytalk.org/t/strange-syntax-sugar/37142/2 "2007-04-24T03:37:17Z")

</div>

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  
softiesonrails.com

> **···**
>
> On Apr 23, 7:40 pm, aalfred \<alfred.anzlo...@gmail.com\> wrote:
> 
> > I've recently had to deal with lots of assignments, where most of them  
> > looked  
> > like those two lines:
> > 
> > &nbsp;&nbsp;foo = bar if !bar.nil? # (case 1) or  
> > &nbsp;&nbsp;foo = bar if foo.nil? && !bar.nil? # (case 2)
> > 
> > (I know it could be written using 'unless', but I strongly dislike  
> > 'unless')

---

<div class="post-metadata">

**Author:** ![Jan\_Svitok](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Svitok](https://rubytalk.org/u/Jan_Svitok)\
**Post date:** [24 April 2007 06:56 UTC](https://rubytalk.org/t/strange-syntax-sugar/37142/3 "2007-04-24T06:56:25Z")

</div>

Note thast when bar == false it won't work.

Jano

> **···**
>
> On 4/24/07, Jeff \<cohen.jeff@gmail.com\> wrote:
> 
> > On Apr 23, 7:40 pm, aalfred \<alfred.anzlo...@gmail.com\> 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  
> > softiesonrails.com

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [24 April 2007 07:35 UTC](https://rubytalk.org/t/strange-syntax-sugar/37142/4 "2007-04-24T07:35:06Z")

</div>

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

&nbsp;&nbsp;robert

> **···**
>
> On 24.04.2007 05:37, Jeff wrote:
> 
> > On Apr 23, 7:40 pm, aalfred \<alfred.anzlo...@gmail.com\> wrote:
> > 
> > > I've recently had to deal with lots of assignments, where most of them  
> > > looked  
> > > like those two lines:
> > > 
> > > &nbsp;&nbsp;foo = bar if !bar.nil? # (case 1) or  
> > > &nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![aalfred](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@aalfred](https://rubytalk.org/u/aalfred)\
**Post date:** [24 April 2007 10:40 UTC](https://rubytalk.org/t/strange-syntax-sugar/37142/5 "2007-04-24T10:40:06Z")

</div>

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.

> **···**
>
> On Apr 24, 5:37 am, Jeff \<cohen.j...@gmail.com\> wrote:
> 
> > On Apr 23, 7:40 pm, aalfred \<alfred.anzlo...@gmail.com\> 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.

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada1](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@Nobuyoshi\_Nakada1](https://rubytalk.org/u/Nobuyoshi_Nakada1)\
**Post date:** [26 April 2007 15:24 UTC](https://rubytalk.org/t/strange-syntax-sugar/37142/6 "2007-04-26T15:24:14Z")

</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.

> **···**
>
> --  
> Nobu Nakada
