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 ...

Get Text Processing with Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.