[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: improperly exiting loop with DATA and __END__

Stefan Rusterholz

10/26/2007 10:31:00 PM

Robert Keller wrote:
> while times = DATA.gets.chomp.split(",")

May I link to this thread in a possible RCR for a -> operator (which
would solve the problem by doing:
while times = DATA.gets->chomp.split(","))?

A solution that works in current ruby:
while times = DATA.gets
times = times.chomp.split(",")

Regards
Stefan
--
Posted via http://www.ruby-....

2 Answers

Phrogz

10/26/2007 11:00:00 PM

0

DATA.readlines.each{ |line|
times = line.chomp.split(",")
unless times.empty?
#do stuff here
p times
end
}
#=> ["Monday", "07:46", "12:04", "12:45", "17:09"]
#=> ["Tuesday", "08:00", "12:28", "13:21", "18:09"]
#=> ["Wednesday", "07:49", "12:05", "12:58", "18:01"]


__END__
Monday,07:46,12:04,12:45,17:09
Tuesday,08:00,12:28,13:21,18:09
Wednesday,07:49,12:05,12:58,18:01



Robert Klemme

10/27/2007 9:47:00 AM

0

On 27.10.2007 01:00, Phrogz wrote:
> DATA.readlines.each{ |line|
> times = line.chomp.split(",")
> unless times.empty?
> #do stuff here
> p times
> end
> }
> #=> ["Monday", "07:46", "12:04", "12:45", "17:09"]
> #=> ["Tuesday", "08:00", "12:28", "13:21", "18:09"]
> #=> ["Wednesday", "07:49", "12:05", "12:58", "18:01"]
>
>
> __END__
> Monday,07:46,12:04,12:45,17:09
> Tuesday,08:00,12:28,13:21,18:09
> Wednesday,07:49,12:05,12:58,18:01

I believe it's more efficient if you omit the ".readlines".

Kind regards

robert