"Luke Worth" <luke / worth.id.au> schrieb im Newsbeitrag 
news:1120914144.28438.10.camel / fish.bwnet.com.au...
> Hi.
>
> I am wanting to parse large files (of source code) by reading byte by
> byte and converting certain patterns into different strings. I can think
> of a couple of ways to do this (starting with the source code in a
> String):
>     1. String#split(//) -> array, then use Array#shift (maybe slow to
>        convert to array?)
>     2. Keep a counter and use String#[] (which seems unrubyish to me)
>
> Which of these would be preferable? Does anyone know of any better ways?

I'd do none of them.  If you know that your patterns stay on single lines 
and don't extend to following lines I'd read the file line by line and use 
String#gsub.  Something along the lines of

while ( line = gets )
  line.gsub!( /pattern/, 'replacement' )
  # or line.gsub!( /pattern/ ) {|match| create replacement }
  puts line
end

If patterns extend lines I'd first try to slurp the whole file into mem and 
then use gsub (possibly with option /m for multiline).  Only if that does 
not work (because of file size for example) I'd resort to more complicated 
solutions like the ones you described.

Kind regards

    robert