From: Robert Klemme Date: 2008-11-04T02:18:37+09:00 Subject: Re: Clean way of pausing a loop? On 03.11.2008 16:35, Kyle Schmitt wrote: > It's Monday and it's relatively early, so I apologize if this isn't > worded too well. > > Is there a clean way of pausing a loop/printing status every N > iterations? I often use ruby to examine log files in a one-off > fashion, and when dealing with files of 4-10 million lines, this is > the sort of thing that becomes important. Small files are, obviously, > not as big of a deal. You can use ARGF.lineno when reading from ARGF: robert@fussel ~ $ seq 1 20 | ruby -e 'ARGF.each {|l| puts "."; puts ARGF.lineno if ARGF.lineno % 10 == 0}' . . . . . . . . . . 10 . . . . . . . . . . 20 You can use IO.lineno when reading from a stream robert@fussel ~ $ seq 1 20 | ruby -e '$stdin.each {|l| puts "."; puts $stdin.lineno if $stdin.lineno % 10 == 0}' . . . . . . . . . . 10 . . . . . . . . . . 20 Or you can use #each_with_index. robert@fussel ~ $ seq 1 20 | ruby -e '$stdin.each_with_index {|l,i| puts "."; puts i if i % 10 == 0}' . 0 . . . . . . . . . . 10 . . . . . . . . . Or you can use $.: robert@fussel ~ $ seq 1 20 | ruby -ne 'puts "."; puts $. if $. % 10 == 0' . . . . . . . . . . 10 . . . . . . . . . . 20 I'm sure I forgot one or more variants. :-) Cheers robert