Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Ask a nonprogrammer what we software types do all day and their answer will probably involve numbers. It’s true: Programs are often knee deep in numerical values, everything from account balances to the number of milliseconds it takes to store a balance in a database. It ain’t called computing for nothing. The thing that non-coders don’t realize is just how much text processing goes on alongside all the number-crunching. Real-world programs tend to overflow with text, everything from the name of the account owner, to the name of the table that stores the balance, to the name of the program doing the storing. Because text processing is so central to just about every programming problem, every modern programming language comes equipped with some kind of string facility, typically a container that will not just store your text but also help you to fold, bend, and mutilate it.
The place for text in a Ruby program is in instances of the String class. In this chapter we will take a quick tour of Ruby’s strings, starting with the surprising number of ways that you create a string and progressing to a fast look at the String API. We will also explore some of the ways that newcomers to Ruby can come to grief with strings and look at a few of the powerful things that Ruby applications do with just a pinch of string processing.