# Range as conditional expr

**URL:** <https://rubytalk.org/t/range-as-conditional-expr/28813>\
**Category:** ruby-talk\
**Created:** [10 July 2006 13:25 UTC](https://rubytalk.org/t/range-as-conditional-expr/28813 "2006-07-10T13:25:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miroslaw](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@Miroslaw](https://rubytalk.org/u/Miroslaw)\
**Post date:** [10 July 2006 13:25 UTC](https://rubytalk.org/t/range-as-conditional-expr/28813/1 "2006-07-10T13:25:10Z")

</div>

Hi,

I am reading "Programming Ruby" [a.k.a. Pickaxe]  
and I found example

> **···**
>
> ---  
> &nbsp;&nbsp;while gets  
> &nbsp;&nbsp;&nbsp;&nbsp;print if /a/ .. /e/  
> &nbsp;&nbsp;end  
> ---  
> but one does not work. However, after changing it to:  
> ----  
> &nbsp;&nbsp;while gets  
> &nbsp;&nbsp;&nbsp;&nbsp;print if (/a/=~$\_) .. (/e/=~$\_)  
> &nbsp;&nbsp;end  
> ---  
> it works.  
> But, what is the problem with the first version?
> 
> Regards

---

<div class="post-metadata">

**Author:** ![Steve7](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@Steve7](https://rubytalk.org/u/Steve7)\
**Post date:** [10 July 2006 16:15 UTC](https://rubytalk.org/t/range-as-conditional-expr/28813/2 "2006-07-10T16:15:10Z")

</div>

In the Second Edition of the Pickaxe, on page 68 it says:

&nbsp;&nbsp;&nbsp;&nbsp;In older versions of Ruby, bare ranges could be used as  
&nbsp;&nbsp;&nbsp;&nbsp;conditions in if, while, and similar statements. You  
&nbsp;&nbsp;&nbsp;&nbsp;could, for example, have written the previous code  
&nbsp;&nbsp;&nbsp;&nbsp;fragment as

&nbsp;&nbsp;while gets  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print if /start/../end/  
&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;This is no longer supported. Unfortunately, no error is  
&nbsp;&nbsp;&nbsp;&nbsp;raised; the test will simply succeed each time.

Hope this answers your question.

Cheers

omirek10@poczta.onet.pl wrote:

> **···**
>
> > Hi,
> > 
> > I am reading "Programming Ruby" [a.k.a. Pickaxe]  
> > and I found example  
> > ---  
> > &nbsp;&nbsp;while gets  
> > &nbsp;&nbsp;&nbsp;&nbsp;print if /a/ .. /e/  
> > &nbsp;&nbsp;end  
> > ---  
> > but one does not work. However, after changing it to:  
> > ----  
> > &nbsp;&nbsp;while gets  
> > &nbsp;&nbsp;&nbsp;&nbsp;print if (/a/=~$\_) .. (/e/=~$\_)  
> > &nbsp;&nbsp;end  
> > ---  
> > it works.  
> > But, what is the problem with the first version?
> > 
> > Regards

---

<div class="post-metadata">

**Author:** ![Miroslaw](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@Miroslaw](https://rubytalk.org/u/Miroslaw)\
**Post date:** [11 July 2006 12:30 UTC](https://rubytalk.org/t/range-as-conditional-expr/28813/3 "2006-07-11T12:30:10Z")

</div>

Thanks Steve. I need to obtain 2nd edition indeed.

Regards
