Chapter 2. Strings

Ruby is a programmer-friendly language. If you are already familiar with object oriented programming, Ruby should quickly become second nature. If you’ve struggled with learning object-oriented programming or are not familiar with it, Ruby should make more sense to you than other object-oriented languages because Ruby’s methods are consistently named, concise, and generally predictable in their behavior.

Throughout this book, we demonstrate concepts through interactive Ruby sessions. Strings are a good place to start because not only are they a useful data type, they’re also easy to create and use. They provide a simple introduction to Ruby, a point of comparison between Ruby and other languages you might know, and an approachable way to introduce important Ruby concepts like duck typing (see Recipe 2.12), open classes (demonstrated in Recipe 2.10), and symbols (Recipe 2.7).

If you use Mac OS X or a Unix environment with Ruby installed, go to your command line right now and type irb. If you’re using Windows, you can download and install the One-Click Installer from http://rubyinstaller.org, and do the same from a command prompt (you can also run the fxri program, if that’s more comfortable for you). You’ve now entered an interactive Ruby shell, and you can follow along with the code samples in most of this book’s recipes.

Strings in Ruby are much like strings in other dynamic languages like Perl, Python, and PHP. They’re not too much different from strings in ...

Get Ruby Cookbook, 2nd Edition 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.