O'Reilly logo

Text Processing with Ruby by Rob Miller

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Example: Parsing a Config File

We’ve seen the various parts of StringScanner in action, but to get an appreciation for how they work in concert it would be useful to see a slightly broader example.

One situation where you might write a parser is when encountering a new file format, one for which there isn’t a ready-made library that can be used to parse its content. As an example, let’s take the following fictional file format, which in this case stores the configuration of a website:

strscan/config.txt
 
name = "Alice’s website"
 
description = "Alice's personal blog"
 
url = "http://alice.example.com/"
 
public = "true"
 
version = "24"

It’s pretty simple, as file formats go. The name of each configuration variable is at the start of the line; there’s ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required