Help with ruby range used as condition

Hi all,

having read “Programming Ruby: the pragmatic programmer"s Guide” I tried the
following “range”-example mentionned in the LOOPS section:

file = File.open("ordinal)
while file.gets
print if /third/ … /fifth/
end

With the following input file “ordinal” :
first
second
third
fourth
fifth
sixth
seventh
eighth
ninth

the result was :
first
second
third
fourth
fifth
sixth
seventh
eighth
ninth

and NOT :
third
fourth
fifth

as mentionned in the book !?
I tried it with ruby 1.8.1 (windows) and ruby 1.8.0 (Linux) with the same
unexpected result.

Am I doing something wrong here ? Is the range-class broke ?

Any help is greatly appreciated,
thanks
Christian

cg wrote:

Hi all,
(Please give your full name in the from header.)

file = File.open(“ordinal”)
while file.gets
print if /third/ … /fifth/
end

With the following input file “ordinal” :
first
second
third
fourth
fifth
sixth
seventh
eighth
ninth

On my computer it works with ruby1.6, but not with ruby1.8.

···


Malte Milatz, konvinkita esperantisto kaj linuks-uzanto.

cg wrote:

having read “Programming Ruby: the pragmatic programmer"s Guide” I tried the
following “range”-example mentionned in the LOOPS section:

file = File.open("ordinal)
while file.gets
print if /third/ … /fifth/
end

Try:

file = File.open(“ordinal”)
while a = file.gets
print a if (/third/ =~ a) … (/fifth/ =~ a)
end

See http://www.rubygarden.org/ruby?ProgrammingRubyTwo/Expressions#loops

(or go to Programming Ruby when checking things in
Programming Ruby that may have changed from 1.6 to 1.8)

Am I doing something wrong here ? Is the range-class broke ?

I think this is part of the cleaning up of Ruby that Matz is doing, but
I’m not sure. The original code relies on a hidden variable and lots of
magic that can seem too Perlish.

···


([ Kent Dahl ]/)_ ~ [ Kent Dahl - Kent Dahl ]/~
))_student_/(( _d L b_/ Master of Science in Technology )
( __õ|õ// ) ) Industrial economics and technology management (
_
/ö____/ (_engineering.discipline=Computer::Technology)

— Malte Milatz malteDELETETHIS@gmx-topmail.de wrote:

On my computer it works with ruby1.6, but not with ruby1.8.

There was a major discussion about this recently – “save the flip/flop”.
You can search the ruby-lang e-mail archives here:

– Thomas Adam

···

=====
“The Linux Weekend Mechanic” – http://linuxgazette.net
“TAG Editor” – http://linuxgazette.net

“ We’ll just save up your sins, Thomas, and punish
you for all of them at once when you get better. The
experience will probably kill you. :)”

– Benjamin A. Okopnik (Linux Gazette Technical Editor)


Yahoo! Messenger - Communicate instantly…“Ping”
your friends today! Download Messenger Now
http://uk.messenger.yahoo.com/download/index.html