# RCR: Comparing multiple values

**URL:** <https://rubytalk.org/t/rcr-comparing-multiple-values/11216>\
**Category:** ruby-talk\
**Created:** [25 April 2004 16:09 UTC](https://rubytalk.org/t/rcr-comparing-multiple-values/11216 "2004-04-25T16:09:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kristof\_Bastiaensen1](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@Kristof\_Bastiaensen1](https://rubytalk.org/u/Kristof_Bastiaensen1)\
**Post date:** [25 April 2004 16:09 UTC](https://rubytalk.org/t/rcr-comparing-multiple-values/11216/1 "2004-04-25T16:09:05Z")

</div>

Hi list,

wouldn’t it be nice to be able to compare multiple values?  
for example:  
x, y == 1, 2  
#equivalent to x == 1 && y == 2

right now you can do  
[x, y] == [1, 2]  
but IMHO it doesn’t look so nice

Kristof

---

<div class="post-metadata">

**Author:** ![Szymon\_Drejewicz1](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@Szymon\_Drejewicz1](https://rubytalk.org/u/Szymon_Drejewicz1)\
**Post date:** [26 April 2004 06:44 UTC](https://rubytalk.org/t/rcr-comparing-multiple-values/11216/2 "2004-04-26T06:44:04Z")

</div>

Kristof Bastiaensen wrote:

> wouldn’t it be nice to be able to compare multiple values?  
> for example:  
> x, y == 1, 2  
> #equivalent to x == 1 && y == 2

For me that notation has one drawback: what should happend if  
y = 5  
and you’ll write  
[x, y == 1, 2]  
?

Is it an array with three elements  
[x, false, 2]  
?

or an array with one element which has value of “multiple comaprision”  
[false]  
?

> **···**
>
> –  
> Szymon Drejewicz

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [26 April 2004 06:58 UTC](https://rubytalk.org/t/rcr-comparing-multiple-values/11216/3 "2004-04-26T06:58:31Z")

</div>

Szymon Drejewicz wrote:

> Kristof Bastiaensen wrote:
> 
> > wouldn’t it be nice to be able to compare multiple values?  
> > for example:  
> > x, y == 1, 2  
> > #equivalent to x == 1 && y == 2
> 
> For me that notation has one drawback: what should happend if  
> y = 5  
> and you’ll write  
> [x, y == 1, 2]  
> ?
> 
> Is it an array with three elements  
> [x, false, 2]  
> ?
> 
> or an array with one element which has value of “multiple comaprision”  
> [false]  
> ?

Not sure that this shows anything, but the current behavior for =  
instead of == is:

[x, y = 1, 2]  
=\> [0, 1, 2]

So it looks like the array context overrides the parallel assignment?

It’s not a good analogy, though, because == is a method and = is not.

---

<div class="post-metadata">

**Author:** ![Laurent\_Julliard4](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@Laurent\_Julliard4](https://rubytalk.org/u/Laurent_Julliard4)\
**Post date:** [26 April 2004 08:29 UTC](https://rubytalk.org/t/rcr-comparing-multiple-values/11216/4 "2004-04-26T08:29:04Z")

</div>

It should be just ‘false’, as expressed in the first message (i.e.  
x==1 and 5==2)

I mean, if we consider ‘,’ as an expression that creates a list  
object, than 1,2 == could be just some kind of sugar.  
But chosing some kind of precedence would probably be impossible, or  
wqould break many of the actual behaviours.

> **···**
>
> il Mon, 26 Apr 2004 08:39:26 +0200, Szymon Drejewicz [drejewic@wsisiz.edu.pl](mailto:drejewic@wsisiz.edu.pl) ha scritto::
> 
> > Kristof Bastiaensen wrote:
> > 
> > > wouldn’t it be nice to be able to compare multiple values?  
> > > for example:  
> > > x, y == 1, 2  
> > > #equivalent to x == 1 && y == 2
> > 
> > For me that notation has one drawback: what should happend if  
> > y = 5  
> > and you’ll write  
> > [x, y == 1, 2]  
> > ?
> > 
> > Is it an array with three elements  
> > [x, false, 2]  
> > ?
> > 
> > or an array with one element which has value of “multiple comaprision”  
> > [false]  
> > ?

---

<div class="post-metadata">

**Author:** ![Kristof\_Bastiaensen1](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@Kristof\_Bastiaensen1](https://rubytalk.org/u/Kristof_Bastiaensen1)\
**Post date:** [26 April 2004 09:34 UTC](https://rubytalk.org/t/rcr-comparing-multiple-values/11216/5 "2004-04-26T09:34:17Z")

</div>

> > For me that notation has one drawback: what should happend if  
> > y = 5  
> > and you’ll write  
> > [x, y == 1, 2]  
> > ?
> > 
> > Is it an array with three elements  
> > [x, false, 2]  
> > ?
> > 
> > or an array with one element which has value of “multiple comaprision”  
> > [false]  
> > ?

That would be a problem indeed. Note that the same aplies  
to method calling: foo(a, b == 1, 2)

The expansion of (x, y == 1, 2) should then occur only outside  
parameter lists.

> It should be just ‘false’, as expressed in the first message (i.e.  
> x==1 and 5==2)

It surely shouldn’t be false, because that would disable  
comparisons inside parameter lists.

> I mean, if we consider ‘,’ as an expression that creates a list  
> object, than 1,2 == could be just some kind of sugar.  
> But chosing some kind of precedence would probably be impossible, or  
> wqould break many of the actual behaviours.

I wouldn’t consider ‘,’ as an expression that creates any object,  
but rather as a way to pass multiple arguments to methods and  
continuations. This is close to the way it actually is in Ruby.

This would be a syntax extension. However (x, y == exp1, exp2)  
would be different from (x == exp1 && y == exp2) because it would  
evaluate the parameters first, and then compare the results.

Another construct that might be useful:  
x, y += 1, 2  
#(meaning x += 1; y += 2)

---

<div class="post-metadata">

**Author:** ![Kristof\_Bastiaensen1](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@Kristof\_Bastiaensen1](https://rubytalk.org/u/Kristof_Bastiaensen1)\
**Post date:** [26 April 2004 09:49 UTC](https://rubytalk.org/t/rcr-comparing-multiple-values/11216/6 "2004-04-26T09:49:04Z")

</div>

I think the analogy is good enough. Ruby already does some method  
reordering on Arithmetic Operators, so why not on this one?

> **···**
>
> On Mon, 26 Apr 2004 15:58:31 +0900, Joel VanderWerf wrote:
> 
> > Not sure that this shows anything, but the current behavior for =  
> > instead of == is:
> > 
> > [x, y = 1, 2]  
> > =\> [0, 1, 2]
> > 
> > So it looks like the array context overrides the parallel assignment?
> 
> > It’s not a good analogy, though, because == is a method and = is not.
