# "or" and "and"

**URL:** <https://rubytalk.org/t/or-and-and/47970>\
**Category:** ruby-talk\
**Created:** [28 July 2008 10:39 UTC](https://rubytalk.org/t/or-and-and/47970 "2008-07-28T10:39:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![es1](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@es1](https://rubytalk.org/u/es1)\
**Post date:** [28 July 2008 10:39 UTC](https://rubytalk.org/t/or-and-and/47970/1 "2008-07-28T10:39:38Z")

</div>

Hi,

I'm just getting to discover ruby, but I find it very nice programming  
language. I just still don't understand how the "or" and "and" in  
ruby...

I was playing with ruby and for example made a def to print Stem and  
Leaf plot (for those who didn't have a statistics course or slept on  
it, e.g. [http://cnx.org/content/m10157/latest/](http://cnx.org/content/m10157/latest/))

Here is the Beta version of it:

class Array

def n ; self.size ; end

def stem\_and\_leaf(st = 1)  
&nbsp;&nbsp;# if st != (2 or 5 or 10) then ; st = 1 ; end  
&nbsp;&nbsp;k = Hash.new(0)  
&nbsp;&nbsp;self.each {|x| k[x.to\_f] += 1 }  
&nbsp;&nbsp;k = k.sort{|a, b| a[0].to\_f \<=\> b[0].to\_f }  
&nbsp;&nbsp;te\_last = 999999999999999999999999999999999 # impossible number  
&nbsp;&nbsp;puts " Stem and Leaf plot" + " N = " + self.n.to\_s + "  
Steps / " + st.to\_s  
&nbsp;&nbsp;st = 10 / st  
&nbsp;&nbsp;k.each do |key, val|  
&nbsp;&nbsp;&nbsp;&nbsp;te = (key / 10).to\_i  
&nbsp;&nbsp;&nbsp;&nbsp;on = (Math.sqrt((key - te \* 10).to\_i\*\*2)).to\_i  
&nbsp;&nbsp;&nbsp;&nbsp;if te == te\_last and on \< st  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;val.times { print on.to\_s }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;te\_last = te  
&nbsp;&nbsp;&nbsp;&nbsp;elsif te == te\_last and on \> st and on \< st + st  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;val.times { print on.to\_s }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;te\_last = te  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;st += st  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "\n" + "%+6s" % te.to\_s + " : "  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;val.times { print on.to\_s }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;te\_last = te  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

end

It prints:

Stem and Leaf plot N = 15 Steps / 2

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 : 234  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 : 56789  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 : 019 # it shouldn't be like this  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3 : 08 # it shouldn't be like this  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4 : 07 # it shouldn't be like this

Or with different "steps" value:

Stem and Leaf plot N = 15 Steps / 5

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 : 23  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 : 4567  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 : 89  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 : 019 # it shouldn't be like this  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3 : 08 # it shouldn't be like this  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4 : 07 # it shouldn't be like this

(data: 12 13 14 15 16 17 18 19 20 21 29 30 38 40 47)

My questions:

/1 If I just wanted it to work with "st" values: 1, 2, 5, how should  
the "or" statement look like? Ruby don't understand multiple or's (I  
tried using different brackets is several places but it still doesn't  
work).

/2 It feels like Ruby didn't understand 3 and's in the elsif part of  
script. It don't understand the 3rd "and". Why? How to write those  
"and" statements?

Thanks in advance : )

If anyone'd like to use the script feel free, I'd be just grateful to  
if you cite this message as a source ; )

Greetz,  
Timo

---

<div class="post-metadata">

**Author:** ![Max\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Max\_Williams](https://rubytalk.org/u/Max_Williams)\
**Post date:** [28 July 2008 11:01 UTC](https://rubytalk.org/t/or-and-and/47970/2 "2008-07-28T11:01:01Z")

</div>

As far as i know ruby deals with multiple ors and ands perfectly well.  
It may be a precedence mixup though: "or" has lower precedence than  
"||", and similarly, "and" has lower precedence than "&&". 'and' and  
'or' actually have the SAME precedence, which has even more potential  
for error, i think. Using the symbols, && has higher precedence than

> > .

Try it with the symbol versions, ie || and &&, and see if it's still  
broken, maybe?

Also, i've found that bracketing up multiple logical tests helps a lot:  
the optional omission of the brackets seems to lead to interpreter to do  
something different to what you'd expect, sometimes.

So, instead of this

elsif te == te\_last and on \> st and on \< st + st

try this:

elsif (te == te\_last) && (on \> st) && (on \< (st + st))

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Alex\_Gutteridge1](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@Alex\_Gutteridge1](https://rubytalk.org/u/Alex_Gutteridge1)\
**Post date:** [28 July 2008 11:04 UTC](https://rubytalk.org/t/or-and-and/47970/3 "2008-07-28T11:04:25Z")

</div>

This won't fly (as you've noticed). you'll need to do either

if st != 2 or st != 5 or st != 10 then st = 1 end

or more consisely

if not [2,5,10].include? st then st = 1 end

or even

[2,5,10].include? st or st = 1

Alex Gutteridge

Department of Biochemistry  
University of Cambridge

> **···**
>
> On 28 Jul 2008, at 11:39, es\_ wrote:
> 
> > class Array
> > 
> > def n ; self.size ; end
> > 
> > def stem\_and\_leaf(st = 1)  
> > &nbsp;&nbsp;# if st != (2 or 5 or 10) then ; st = 1 ; end

---

<div class="post-metadata">

**Author:** ![es1](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@es1](https://rubytalk.org/u/es1)\
**Post date:** [28 July 2008 13:39 UTC](https://rubytalk.org/t/or-and-and/47970/4 "2008-07-28T13:39:36Z")

</div>

Hi,

I've spotted the err in and's by printing the st values:

Stem and Leaf plot N = 15 Steps / 5  
(st =2)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 : 2(st =2)3(st =4)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 : 4(st =4)5(st =8)6(st =8)7(st =8)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 : 8(st =8)9(st =16)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 : 0(st =16)1(st =16)9(st =16)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3 : 0(st =16)8(st =16)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4 : 0(st =16)7

...stupid mistake, I forgot that it sums it into infinity.

In the "or" example it was just my logical mistake. I mistaken "or"  
with "neither"...

Greetz and thanks for help!  
Timo

---

<div class="post-metadata">

**Author:** ![Max\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Max\_Williams](https://rubytalk.org/u/Max_Williams)\
**Post date:** [28 July 2008 11:11 UTC](https://rubytalk.org/t/or-and-and/47970/5 "2008-07-28T11:11:08Z")

</div>

Note to self - read question properly before answering 🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![es1](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@es1](https://rubytalk.org/u/es1)\
**Post date:** [28 July 2008 12:44 UTC](https://rubytalk.org/t/or-and-and/47970/6 "2008-07-28T12:44:48Z")

</div>

Thanks for help!

The problem is that 3 or's don't work:

?\> st = 1  
=\> 1

> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if st != 2 or st != 5 or st != 10 then ; st = 1 ; end

=\> 1

> >

?\> st = 2  
=\> 2

> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if st != 2 or st != 5 or st != 10 then ; st = 1 ; end

=\> 1

> >

?\> st = 2  
=\> 2

> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if st != 2 or st != 5 or st != 10 then ; st = 1 ; end

=\> 1

> > st = 5

=\> 5

> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if st != 2 or st != 5 or st != 10 then ; st = 1 ; end

=\> 1

> > st = 10

=\> 10

> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if st != 2 or st != 5 or st != 10 then ; st = 1 ; end

=\> 1

Or with extra brackets:

> > if (st != 2) or (st != 5) or (st != 10) then ; st = 1 ; end

=\> 1

Hopefully the "include' option works fine and is beautiful, that's a  
great example of simplicity of Ruby I'm getting to appreciate.

And about the brackets, Max Williams wrote, it doesn't work either. I  
tried if before. I also know the difference on "and" and "&&", I  
revived some articles on that while looking for some info.

Greetz,  
Timo

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [28 July 2008 23:01 UTC](https://rubytalk.org/t/or-and-and/47970/7 "2008-07-28T23:01:30Z")

</div>

And as for WHY it doesn't fly.

The expression

&nbsp;&nbsp;2 or 5 or 10

evaluates to

&nbsp;&nbsp;2

since anything but nil or false is 'truthy' in ruby and or returns its left  
hand operand it it's truthy otherwise it returns the evaluation of its right  
hand operand.

So

&nbsp;&nbsp;&nbsp;if st !=(2 or 5 or 10) then; st = 1;end

might as well be

&nbsp;&nbsp;if st != 2 then; st = 1;end

> **···**
>
> On Mon, Jul 28, 2008 at 7:04 AM, Alex Gutteridge \<alexg@ruggedtextile.com\>wrote:
> 
> > On 28 Jul 2008, at 11:39, es\_ wrote:
> > 
> > class Array
> > 
> > > def n ; self.size ; end
> > > 
> > > def stem\_and\_leaf(st = 1)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# if st != (2 or 5 or 10) then ; st = 1 ; end
> > 
> > This won't fly (as you've noticed).
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [28 July 2008 12:50 UTC](https://rubytalk.org/t/or-and-and/47970/8 "2008-07-28T12:50:12Z")

</div>

Hi --

> **···**
>
> On Mon, 28 Jul 2008, es\_ wrote:
> 
> > Thanks for help!
> > 
> > The problem is that 3 or's don't work:
> > 
> > ?\> st = 1  
> > =\> 1
> > 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if st != 2 or st != 5 or st != 10 then ; st = 1 ; end
> > 
> > =\> 1
> > 
> > > >
> > 
> > ?\> st = 2  
> > =\> 2
> > 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if st != 2 or st != 5 or st != 10 then ; st = 1 ; end
> > 
> > =\> 1
> > 
> > > >
> > 
> > ?\> st = 2  
> > =\> 2
> > 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if st != 2 or st != 5 or st != 10 then ; st = 1 ; end
> > 
> > =\> 1
> > 
> > > > st = 5
> > 
> > =\> 5
> > 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if st != 2 or st != 5 or st != 10 then ; st = 1 ; end
> > 
> > =\> 1
> > 
> > > > st = 10
> > 
> > =\> 10
> > 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if st != 2 or st != 5 or st != 10 then ; st = 1 ; end
> > 
> > =\> 1
> 
> It looks OK to me. What isn't working here?
> 
> David
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;\* Advancing With Rails August 18-21 Edison, NJ  
> &nbsp;&nbsp;\* Co-taught by D.A. Black and Erik Kastner  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![es1](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@es1](https://rubytalk.org/u/es1)\
**Post date:** [28 July 2008 12:59 UTC](https://rubytalk.org/t/or-and-and/47970/9 "2008-07-28T12:59:39Z")

</div>

> It looks OK to me. What isn't working here?

It checks if st is 2, 5 or 10. I set st to 5 and it says, "wrong"  
while it should say "correct".

Greetz,  
Timo

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [28 July 2008 13:04 UTC](https://rubytalk.org/t/or-and-and/47970/10 "2008-07-28T13:04:47Z")

</div>

es\_ wrote:

> It checks if st is 2, 5 or 10. I set st to 5 and it says, "wrong"  
> while it should say "correct".

No, it checks whether st is not 2, st is not 5 or st is not 10.  
When st is 5 it is still not 10 and not 2. So two out of three conditions are  
true. And with or it suffices if one of the conditions is true.

HTH,  
Sebastian

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826
