[ruby-talk:444418] logic in Ruby

Hi

p(1 and 2 or 3 and 4)

1. how many brackets do we need?

2. in my opinion the result should be 2, not 4.

What do you think?

Andreas

···

______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

There are orders of precedence at work here, but also some subtle ones that might not be clear.

`and` and `&&` have different precedence. Likewise `or` and `||`.

And I cannot count the number of times that I have tried to be all minimal and introduced a logical error in my code. Only staring at it long enough made it clear to me that a whole branch of my comparison was being ignored.

I personally tend to put lots more parentheses in than might strictly be needed, and I treat them as if they were comments -- they explain visually what I am imagining the grouping to be. And sometimes that turns out to be necessary in order for it work the way I expect it to.

A good question to ask yourself: would this be clearer as two lines? What would those two lines be?

Could you use `return foo if 1 and 2` to short-circuit it?

Not an answer, but maybe a way for you to interrogate the question from a different angle.

Walter

···

On Feb 17, 2024, at 2:04 PM, Information via ruby-talk <ruby-talk@ml.ruby-lang.org> wrote:

Hi

p(1 and 2 or 3 and 4)

1. how many brackets do we need?

2. in my opinion the result should be 2, not 4.

What do you think?

Andreas

______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

In general, you should always use `||` and `&&`. Except for expressions
like follows:

  system "false" or raise "False return value"

Note how you don't need any parentheses in this case, but you would if
you used `||` or `&&`. Think of `or` and `and` more like how you think
about `if` or `unless` (as in, all 4 are for control flow).

In fact, all `||`, `&&`, `or`, `and` under the hood work exactly the
same (except for the priority). It's rather that the verbose ones were
designed for the usecase I wrote about.

···

On Sat, 2024-02-17 at 20:04 +0100, Information via ruby-talk wrote:

Hi

p(1 and 2 or 3 and 4)

1. how many brackets do we need?

2. in my opinion the result should be 2, not 4.

What do you think?

Andreas

______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info --
Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

Hi
p(1 and 2 or 3 and 4)
1. how many brackets do we need?

none if you know the order :slight_smile:

2. in my opinion the result should be 2, not 4.
What do you think?

1 and 2 or 3 and 4

=> 4

evaluation left to right

1 && 2 || 3 && 4

=> 2

evaluation 1st: 1 && 2 => 2
evaluation 2nd: 3 && 4 => 4
evaluation last: 2 || 4 => 2

"or" and "and" have same priority.
"&&" precedes "||".

check: precedence - Documentation for Ruby 3.4

kind regards --botp

Andreas

···

On Sun, Feb 18, 2024 at 3:05 AM Information via ruby-talk < ruby-talk@ml.ruby-lang.org> wrote:

    Hi
    p(1 and 2 or 3 and 4)
    1. how many brackets do we need?

none if you know the order :slight_smile:

Maybe there is a misunderstanding:
p(--"--) doesnt work at all (Syntax error)
p((--"--) does the job. (Maybe for this the subject was misleading_

    2. in my opinion the result should be 2, not 4.
    What do you think?

> 1 and 2 or 3 and 4
=> 4

evaluation left to right

> 1 && 2 || 3 && 4
=> 2

evaluation 1st: 1 && 2 => 2
evaluation 2nd: 3 && 4 => 4
evaluation last: 2 || 4

So why should be there another result if using and/or instead of &&/|| ?

(1 and 2 or 3 and 4) != (1 && 2 || 3 && 4)

- That shouldn't be the case!

Andreas

···

Am 19.02.24 um 11:22 schrieb botp:

On Sun, Feb 18, 2024 at 3:05 AM Information via ruby-talk > <ruby-talk@ml.ruby-lang.org <mailto:ruby-talk@ml.ruby-lang.org>> wrote:

Unlike Python, the "word" operators in Ruby aren't logic operators, they're
flow control. They way they work is basically a carry-over from the way
that Perl handled them. They serve different purposes, and are not
equivalent.

···

On Sat, Feb 24, 2024, 03:24 Information via ruby-talk < ruby-talk@ml.ruby-lang.org> wrote:

Am 19.02.24 um 11:22 schrieb botp:

On Sun, Feb 18, 2024 at 3:05 AM Information via ruby-talk < > ruby-talk@ml.ruby-lang.org> wrote:

Hi
p(1 and 2 or 3 and 4)
1. how many brackets do we need?

none if you know the order :slight_smile:

Maybe there is a misunderstanding:
p(--"--) doesnt work at all (Syntax error)
p((--"--) does the job. (Maybe for this the subject was misleading_

2. in my opinion the result should be 2, not 4.

What do you think?

> 1 and 2 or 3 and 4
=> 4

evaluation left to right

> 1 && 2 || 3 && 4
=> 2

evaluation 1st: 1 && 2 => 2
evaluation 2nd: 3 && 4 => 4
evaluation last: 2 || 4

So why should be there another result if using and/or instead of &&/|| ?

(1 and 2 or 3 and 4) != (1 && 2 || 3 && 4)

- That shouldn't be the case!

Andreas
______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info --
Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

So why should be there another result if using and/or instead of &&/||?

https://www.prestonlee.com/2010/08/04/ruby-on-the-perl-origins-of-and-versus-and-and-or/

(1 and 2 or 3 and 4) != (1 && 2 || 3 && 4)
- That shouldn't be the case!

Perhaps not. I don't know why matz gave the "and"/"or" keywords the same
precedence rather than making "and" higher precedence than "or", but that's
been the case in ruby forever.

Perhaps it was a mistake since perl does maintain the relative precedence:

$ perl -e 'print(1 && 2 || 3 && 4);'
2
$ perl -e 'print(1 and 2 or 3 and 4);'
2

Or, perhaps matz felt that since the keyword form is intended for flow-control,
the flat precedence was appropriate.

$ git log -p parse.y
[...]
commit 3db12e8b236ac8f88db8eb4690d10e4a3b8dbcd4 (tag: v1_0_r2)
Author: matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>

    Initial revision
    
    git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

diff --git a/parse.y b/parse.y
new file mode 100644
index 0000000000..02ff720bb3
--- /dev/null
+++ b/parse.y
@@ -0,0 +1,3717 @@
+/************************************************

···

On Sat, Feb 24, 2024, at 6:22 AM, Information via ruby-talk wrote:
Date: Fri Jan 16 12:13:05 1998 +0000
+
+ parse.y -
+
+ $Author$
+ $Date$
+ created at: Fri May 28 18:02:42 JST 1993
+
+ Copyright (C) 1993-1996 Yukihiro Matsumoto
+
+************************************************/
[...]
+%left OR AND
[...]
+%left OROP
+%left ANDOP
[...]
______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

> 1 and 2 or 3 and 4
=> 4

evaluation left to right

> 1 && 2 || 3 && 4
=> 2

evaluation 1st: 1 && 2 => 2
evaluation 2nd: 3 && 4 => 4
evaluation last: 2 || 4 => 2

not quite. the LHS isn't evaluated:

#>>> 1 && 2 || raise("NO!") && 4
# => 2

"or" and "and" have same priority.
"&&" precedes "||".

put another way, slightly more visual:

% rake debug R="1 && 2 || 3 && 4"
s(:or,
s(:and, s(:lit, 1), s(:lit, 2)),
s(:and, s(:lit, 3), s(:lit, 4)))

which is what everyone expects, versus:

% rake debug R="1 and 2 or 3 and 4"
s(:and,
s(:or,
  s(:and, s(:lit, 1), s(:lit, 2)),
  s(:lit, 3)),
s(:lit, 4))

which is the same as:

···

On Feb 19, 2024, at 02:22, botp via ruby-talk <ruby-talk@ml.ruby-lang.org> wrote:

% rake debug R="((1 and 2) or 3) and 4"
s(:and,
s(:or,
  s(:and, s(:lit, 1), s(:lit, 2)),
  s(:lit, 3)),
s(:lit, 4))

______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org