You may need to adjust the regex a little, but that should match 8
charicters at a time…
I think if the length isn’t a multiple of 8 it may just ignore the
last bunch of chars… but you get the idea
For that not to be ignored, use {1,8} instead of just {8}. As a minor
note, OP wanted 80, not 8
You may need to adjust the regex a little, but that should match 8
charicters at a time…
I think if the length isn’t a multiple of 8 it may just ignore the
last bunch of chars… but you get the idea
edit the regex as needed.
PS: Scan is awesome when you need to do some quick an dirty log
analysis
data=File.read(foo).scan(regex)
Almost. The latter produces a string with “ ” after every 80 chars,
while the former returns an array of strings 86 chars long each. What is
more appropriate depends on a context. Say, you may want to decorate
chunks even further by passing them through some other methods (on the
other hand, grep with a block may be used for that as well).
ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-darwin8.11.1]
user system total real
with_scan 0.750000 0.040000 0.790000 ( 0.851104)
with_gsub 0.840000 0.060000 0.900000 ( 0.950050)
with_insert 2.000000 0.050000 2.050000 ( 2.210962)
There is an inconsistency in your test: the gsub version creates the
second argument to gsub over and over again. You should rather define a
constant for that as well. You could do that as well for the sanity
check string.
With the attached changed script this is what I get:
There is an inconsistency in your test: the gsub version creates the second
argument to gsub over and over again. You should rather define a constant
for that as well. You could do that as well for the sanity check string.
Yes, thank you for the pointers. In this case the change didn’t seem
to make any significant difference on my box, but the wisdom is
gratefully accepted.
Cheers,
lasitha.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.