Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringScanner scanning IO instead of a string

Tags:

io

ruby

I've got a parser written using ruby's standard StringScanner. It would be nice if I could use it on streaming files. Is there an equivalent to StringScanner that doesn't require me to load the whole string into memory?

like image 359
jes5199 Avatar asked Mar 16 '10 23:03

jes5199


1 Answers

You might have to rework your parser a bit, but you can feed lines from a file to a scanner like this:

File.open('filepath.txt', 'r') do |file|
  scanner = StringScanner.new(file.readline)
  until file.eof?
    scanner.scan(/whatever/)
    scanner << file.readline
  end
end
like image 133
mckeed Avatar answered Dec 04 '22 14:12

mckeed