Range as conditional expr

Hi,

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

while gets
print if /a/ … /e/
end

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

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

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

Regards

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/
end

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

Hope this answers your question.

Cheers

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

Regards

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