# Pickaxe page 342, Ranges in Boolean Expressions --I don't understand

**URL:** <https://rubytalk.org/t/pickaxe-page-342-ranges-in-boolean-expressions-i-dont-understand/18090>\
**Category:** ruby-talk\
**Created:** [3 May 2005 16:44 UTC](https://rubytalk.org/t/pickaxe-page-342-ranges-in-boolean-expressions-i-dont-understand/18090 "2005-05-03T16:44:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gunnar](https://avatars.discourse-cdn.com/v4/letter/g/ed655f/32.png) [@Gunnar](https://rubytalk.org/u/Gunnar)\
**Post date:** [3 May 2005 16:44 UTC](https://rubytalk.org/t/pickaxe-page-342-ranges-in-boolean-expressions-i-dont-understand/18090/1 "2005-05-03T16:44:59Z")

</div>

Hi all  
I can not make sense of the boolean ranges. The example  
&nbsp;&nbsp;&nbsp;(11..20).collect {|i| (i%4==0.. i%3==0)?i:nil} =\> [nil, 12, nil, nil,  
nil, 16, 17, 18, nil, 20].

I can figure out all numbers except the '18', which in my opinion should be  
a 'nil' since i%3==0 is true and would cause the state to change from SET  
to UNSET. Is the state machine Fig 22.1 correct?  
Gunnar

---

<div class="post-metadata">

**Author:** ![Jacob\_Fugal](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@Jacob\_Fugal](https://rubytalk.org/u/Jacob_Fugal)\
**Post date:** [3 May 2005 17:02 UTC](https://rubytalk.org/t/pickaxe-page-342-ranges-in-boolean-expressions-i-dont-understand/18090/2 "2005-05-03T17:02:49Z")

</div>

It appears to me that the second condition in the range UNSETs, but  
inclusively (which makes sense for the semantics of '..'). That's why,  
for example, 12 is printed at all... 12%4==0, so it SETs, retval is  
set to true since it is SET, then 12%3==0 so it UNSETs. But the retval  
is already set, and it returns true, thus printing 12.

In the same way, when we get to 18, the state machine is already SET  
(since 16). retval is set to true since it is SET, then 18%3==0 so it  
unsets. But just like with 12, retval is already set, so it returns  
true and prints 18.

For more information see [ruby-talk:91137].

Jacob Fugal

[ruby-talk:91137] [http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/91137](http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/91137)

> **···**
>
> On 5/3/05, Gunnar \<gunnar.larsson2@comhem.se\> wrote:
> 
> > Hi all  
> > I can not make sense of the boolean ranges. The example  
> > &nbsp;&nbsp;&nbsp;(11..20).collect {|i| (i%4==0.. i%3==0)?i:nil} =\> [nil, 12, nil, nil,  
> > nil, 16, 17, 18, nil, 20].
> > 
> > I can figure out all numbers except the '18', which in my opinion should be  
> > a 'nil' since i%3==0 is true and would cause the state to change from SET  
> > to UNSET. Is the state machine Fig 22.1 correct?

---

<div class="post-metadata">

**Author:** ![Gunnar](https://avatars.discourse-cdn.com/v4/letter/g/ed655f/32.png) [@Gunnar](https://rubytalk.org/u/Gunnar)\
**Post date:** [3 May 2005 17:29 UTC](https://rubytalk.org/t/pickaxe-page-342-ranges-in-boolean-expressions-i-dont-understand/18090/3 "2005-05-03T17:29:45Z")

</div>

Jacob Fugal wrote:

> **···**
>
> > On 5/3/05, Gunnar \<gunnar.larsson2@comhem.se\> wrote:
> > 
> > > Hi all  
> > > I can not make sense of the boolean ranges. The example  
> > > &nbsp;&nbsp;&nbsp;(11..20).collect {|i| (i%4==0.. i%3==0)?i:nil} =\> [nil, 12, nil, nil,  
> > > nil, 16, 17, 18, nil, 20].
> > > 
> > > I can figure out all numbers except the '18', which in my opinion should  
> > > be a 'nil' since i%3==0 is true and would cause the state to change from  
> > > SET to UNSET. Is the state machine Fig 22.1 correct?
> > 
> > It appears to me that the second condition in the range UNSETs, but  
> > inclusively (which makes sense for the semantics of '..'). That's why,  
> > for example, 12 is printed at all... 12%4==0, so it SETs, retval is  
> > set to true since it is SET, then 12%3==0 so it UNSETs. But the retval  
> > is already set, and it returns true, thus printing 12.
> > 
> > In the same way, when we get to 18, the state machine is already SET  
> > (since 16). retval is set to true since it is SET, then 18%3==0 so it  
> > unsets. But just like with 12, retval is already set, so it returns  
> > true and prints 18.
> > 
> > For more information see [ruby-talk:91137].
> > 
> > Jacob Fugal
> > 
> > [ruby-talk:91137]  
> > [[http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/91137](http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/91137)
> 
> OK.  
> Thank you for answering so very quickly  
> Gunnar

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [3 May 2005 17:49 UTC](https://rubytalk.org/t/pickaxe-page-342-ranges-in-boolean-expressions-i-dont-understand/18090/4 "2005-05-03T17:49:38Z")

</div>

Gunnar wrote:

> Jacob Fugal wrote:
> 
> > > Hi all  
> > > I can not make sense of the boolean ranges. The example  
> > > &nbsp;&nbsp;&nbsp;(11..20).collect {|i| (i%4==0.. i%3==0)?i:nil} =\> [nil, 12, nil,  
> > > nil, nil, 16, 17, 18, nil, 20].
> > > 
> > > I can figure out all numbers except the '18', which in my opinion  
> > > should be a 'nil' since i%3==0 is true and would cause the state to  
> > > change from SET to UNSET. Is the state machine Fig 22.1 correct?
> > 
> > It appears to me that the second condition in the range UNSETs, but  
> > inclusively (which makes sense for the semantics of '..'). That's  
> > why, for example, 12 is printed at all... 12%4==0, so it SETs,  
> > retval is set to true since it is SET, then 12%3==0 so it UNSETs.  
> > But the retval is already set, and it returns true, thus printing 12.
> > 
> > In the same way, when we get to 18, the state machine is already SET  
> > (since 16). retval is set to true since it is SET, then 18%3==0 so it  
> > unsets. But just like with 12, retval is already set, so it returns  
> > true and prints 18.
> > 
> > For more information see [ruby-talk:91137].
> > 
> > Jacob Fugal
> > 
> > [ruby-talk:91137]  
> > [[http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/91137](http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/91137)
> 
> OK.  
> Thank you for answering so very quickly  
> Gunnar

It's historic: Perl does it the same way. It's useful because you can  
easily match ranges (for example of line comments, that start with  
"=begin" and end with "=end" line) etc.

> > s=\<\<EOS

1  
2  
3  
4  
5  
6  
6  
EOS  
=\> "1\n2\n3\n4\n5\n6\n6\n"

> >

?\> s.each do |item|  
?\> puts /3/ =~ item .. /5/ =~ item ? "inside:"+item : "outside:"+item

> > end

outside:1  
outside:2  
inside:3  
inside:4  
inside:5  
outside:6  
outside:6  
=\> "1\n2\n3\n4\n5\n6\n6\n"

> >

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > > On 5/3/05, Gunnar \<gunnar.larsson2@comhem.se\> wrote:
