# When should one use "and" and "or"

**URL:** <https://www.ruby-forum.com/t/when-should-one-use-and-and-or/182948>\
**Category:** Ruby\
**Created:** [February 5, 2010, 5:06am UTC](https://www.ruby-forum.com/t/when-should-one-use-and-and-or/182948 "2010-02-05T05:06:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [February 5, 2010, 5:06am UTC](https://www.ruby-forum.com/t/when-should-one-use-and-and-or/182948/1 "2010-02-05T05:06:35Z")

</div>

Hi All,

just got hit w the ff

> case  
> when x==1 or x==2  
> print “yeoh!”  
> end  
> SyntaxError: compile error  
> (irb):17: syntax error, unexpected kOR, expecting kTHEN or ‘:’ or ‘\n’  
> or ‘;’  
> when x==1 or x==2  
> ^

“or” and “and” both exhibit the behaviour in ruby1.8/1.9.

workaround  
1 put parens around the condition (not again)  
2 replace or/and with ||/&&

i believe there are other similar quirks when using “and/or”…  
has ruby relegated the use of “and/or”…?

best regards -botp

---

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [February 5, 2010, 6:45am UTC](https://www.ruby-forum.com/t/when-should-one-use-and-and-or/182948/2 "2010-02-05T06:45:29Z")

</div>

On Feb 4, 9:55 pm, botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi All,
> 
> just got hit w the ff
> 
> > case  
> > when x==1 or x==2  
> > print “yeoh!”  
> > end

x = 5  
==\>5  
case x  
when 2,3  
p ‘no’  
when 4,5  
p ‘yes’  
end  
“yes”  
==\>nil

---

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [February 5, 2010, 8:14am UTC](https://www.ruby-forum.com/t/when-should-one-use-and-and-or/182948/3 "2010-02-05T08:14:01Z")

</div>

botp wrote:

> i believe there are other similar quirks when using “and/or”…  
> has ruby relegated the use of “and/or”…?

No, but it’s essentially inherited from perl. These operators have  
extremely low precedence, so without parens your statement was being  
parsed as something like

(when x==1) or x==2

I think the typical perl use is something like:

open F, “foo” or die “Oops”;

If you use || then you need extra params so you don’t get  
open(F, “foo” || die “Oops”);

But if you rely on poetry mode like this it may still trip you up. IMO  
you’re better off with:

a = (some-expr) || (raise “Oops”)  
a = (some-expr) || raise(“Oops”)

---

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [February 5, 2010, 4:05pm UTC](https://www.ruby-forum.com/t/when-should-one-use-and-and-or/182948/4 "2010-02-05T16:05:36Z")

</div>

On Fri, Feb 5, 2010 at 3:14 PM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> No, but it’s essentially inherited from perl. These operators have  
> extremely low precedence, so without parens your statement was being  
> parsed as something like
> 
> (when x==1) or x==2
> 
> I think the typical perl use is something like:  
> …

but brian, i’m still inside the “case” block. can’t ruby sense that?

best regards -botp

---

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [February 5, 2010, 6:56am UTC](https://www.ruby-forum.com/t/when-should-one-use-and-and-or/182948/5 "2010-02-05T06:56:37Z")

</div>

On Fri, Feb 5, 2010 at 1:45 PM, w\_a\_x\_man [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
#\> x = 5  
#\> ==\>5  
#\> case x  
#\> when 2,3  
#\> p ‘no’  
#\> when 4,5  
#\> p ‘yes’  
#\> end

of course i know that 😉  
im talking about the second type of “case”, eg

> case  
> when x.baz and y.foo  
> …

best regards -botp

---

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [February 5, 2010, 8:54pm UTC](https://www.ruby-forum.com/t/when-should-one-use-and-and-or/182948/6 "2010-02-05T20:54:46Z")

</div>

botp wrote:

> but brian, i’m still inside the “case” block. can’t ruby sense that?  
> And shouldn’t ‘when’ have the same precedence as ‘if’?

At least for ‘or’, you can use a comma instead:

case  
when x==1, x==2; puts “1 or 2”  
end

but it doesn’t read as well, and there’s nothing that works like this  
for ‘and’.

---

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [February 5, 2010, 6:06pm UTC](https://www.ruby-forum.com/t/when-should-one-use-and-and-or/182948/7 "2010-02-05T18:06:21Z")

</div>

botp wrote:

> > ï¿½(when x==1) or x==2
> > 
> > I think the typical perl use is something like:  
> > …
> 
> but brian, i’m still inside the “case” block. can’t ruby sense that?

I agree, but in the absence of a formal grammar you’ll have to look at  
the implementation.
