Range as conditional expr


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

while gets
print if /a/ … /e/

but one does not work. However, after changing it to:

while gets
print if (/a/=~$) … (/e/=~$)

it works.
But, what is the problem with the first version?


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

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

while gets
    print if /start/../end/

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

Hope this answers your question.


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


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs