Chapter 9. Database facilities and techniques
This chapter covers
- Using plain text data storage
- Automating contacts in an address book
- Using Ruby’s API for gdbm
- Using relational databases
So far we’ve taken a look at a number of technologies and how to use them with Ruby. This chapter will introduce key database tools available with, and for, Ruby. We’ll take a broad view of what it means for something to be a database. Our working definition will be that a database is a storage unit external to your program where data can be stored. This definition includes highly evolved structures like relational databases, but also includes, potentially, plain-text files.
We’ll also stipulate that the stored data has to have some intelligence about itself. A file containing a stream of words—though it may be loaded into a program as a string or an array—will not count as a database, because the file itself does not preserve any information about the structure of the data.
A flat file in YAML format, however, will count as a database, under our definition. As you’ll see, it’s possible and not even terribly difficult to wrap a YAML file in a workable API and reap the benefits of addressing your data structurally while also having access to it in text form. Text files are not the whole story, of course. Ruby ships with several flavors of the Berkeley database (DBM) system. We’ll also look at tools available for creating and manipulating relational databases in Ruby.