# Binary Logic, Bug or Feature?

**URL:** <https://www.ruby-forum.com/t/binary-logic-bug-or-feature/121460>\
**Category:** Ruby\
**Created:** [November 28, 2007, 5:34am UTC](https://www.ruby-forum.com/t/binary-logic-bug-or-feature/121460 "2007-11-28T05:34:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Malcolm\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/54ee81/32.png) [@Malcolm\_L](https://www.ruby-forum.com/u/Malcolm_L)\
**Post date:** [November 28, 2007, 5:34am UTC](https://www.ruby-forum.com/t/binary-logic-bug-or-feature/121460/1 "2007-11-28T05:34:25Z")

</div>

Hi Guys,

I have been pulling my hair out for the last 20 minutes trying to find  
out what is wrong in my app that has some pretty basic binary logic.  
Here is a concise example in an irb session that shows my problem  
(certainly, what I was working on wasn’t nearly as simple the sides of  
AND were variables etc.):

?\> x = true and true  
=\> true

> > x  
> > =\> true

(this is what I expect, true and true = true)

> > x = true and false  
> > =\> false
> 
> > x  
> > =\> true

But what I’ve got here is “true and false” == false, but it assigns  
true to x. So true and false == false, but really == true… Have I  
lost my mind, should it do this? Does it do it for anyone else? It  
seems weird to me since the first example does what I expect, but the  
second doesn’t…

So now I’m reduced to doing something like:  
if (true and false) then x = true else x = false end  
which is kind of annoying, and not very ruby-esque IMO.

Any advice appreciated!

$ ruby -v  
ruby 1.8.5 (2006-08-25) [i486-linux]

OS is Ubuntu 7.04.

(I know, old ruby ver - its just the stock ubuntu one)

THANKS!

- Malcolm.

---

<div class="post-metadata">

**Author:** ![Malcolm\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/54ee81/32.png) [@Malcolm\_L](https://www.ruby-forum.com/u/Malcolm_L)\
**Post date:** [November 28, 2007, 5:37am UTC](https://www.ruby-forum.com/t/binary-logic-bug-or-feature/121460/2 "2007-11-28T05:37:56Z")

</div>

Hi,

In message “Re: Binary Logic, Bug or Feature?”  
on Wed, 28 Nov 2007 13:33:36 +0900, “Malcolm L.”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|I have been pulling my hair out for the last 20 minutes trying to find  
|out what is wrong in my app that has some pretty basic binary logic.  
|Here is a concise example in an irb session that shows my problem  
|(certainly, what I was working on wasn’t nearly as simple the sides of  
|AND were variables etc.):  
|  
|?\> x = true and true  
|=\> true  
|\>\> x  
|=\> true  
|  
|(this is what I expect, true and true = true)  
|  
|\>\> x = true and false  
|=\> false  
|\>\> x  
|=\> true  
|  
|But what I’ve got here is “true and false” == false, but it assigns  
|true to x. So true and false == false, but really == true… Have I  
|lost my mind, should it do this? Does it do it for anyone else? It  
|seems weird to me since the first example does what I expect, but the  
|second doesn’t…

Precedence.

x = true and false

is parsed as

(x = true) and false

not

x = (true and false)

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![Malcolm\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/54ee81/32.png) [@Malcolm\_L](https://www.ruby-forum.com/u/Malcolm_L)\
**Post date:** [November 28, 2007, 5:44am UTC](https://www.ruby-forum.com/t/binary-logic-bug-or-feature/121460/3 "2007-11-28T05:44:21Z")

</div>

It should work if you use && instead of and. Not sure why that is, but  
I’m experencing the same thing as you when doing x = true and false,  
but it works as expected when using x = true && false.

On 11/28/07, Malcolm L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> true  
> But what I’ve got here is “true and false” == false, but it assigns

–

“Every child has many wishes. Some include a wallet, two chicks and a  
cigar, but that’s another story.”

---

<div class="post-metadata">

**Author:** ![Malcolm\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/54ee81/32.png) [@Malcolm\_L](https://www.ruby-forum.com/u/Malcolm_L)\
**Post date:** [November 28, 2007, 5:58am UTC](https://www.ruby-forum.com/t/binary-logic-bug-or-feature/121460/4 "2007-11-28T05:58:58Z")

</div>

On Nov 28, 2007 5:37 PM, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> x = (true and false)
> 
> ```
> matz.
> 
> ```

Of course, that makes perfect sense - I guess I am still getting used to  
Ruby’s awesome level of flexibility!

Thanks a lot for the quick reply matz, btw - big fan of your work :).

Also, thanks Christian - I will make use of the && in the future!

Thanks,  
Malcolm.

---

<div class="post-metadata">

**Author:** ![Malcolm\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/54ee81/32.png) [@Malcolm\_L](https://www.ruby-forum.com/u/Malcolm_L)\
**Post date:** [November 28, 2007, 7:30am UTC](https://www.ruby-forum.com/t/binary-logic-bug-or-feature/121460/5 "2007-11-28T07:30:42Z")

</div>

On Nov 27, 10:43 pm, Christian [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It should work if you use && instead of and. Not sure why that is, but  
> I’m experencing the same thing as you when doing x = true and false,  
> but it works as expected when using x = true && false.

This also happens in Perl and maybe other similar languages. The  
English versions of the logical operators (and, or, not) have lower  
precedence than the symbolic versions (&&, ||, !). Why? I don’t know -  
the languages were just designed that way.

Jeremy
