Chapter 3Letters

We’ve learned all about numbers, but what about letters? Words? Text?

We refer to groups of letters in a program as strings. (You can think of beads with letters on them being strung together.) To make it easier to see just what part of the code is in a string, I’ll color strings 'like this'. Here are some strings:

'Hello.'
'Ruby rocks.'
'Nobody deserves a mime, Buffy.'
'Snoopy says #%^?&*@! when he stubs his toe.'
' '
''

As you can see, strings can have punctuation, digits, symbols, and spaces in them…more than just letters. That last string doesn’t have anything in it at all; we call that an empty string.

We used puts to print numbers; let’s try it with some strings:

puts ​'Hello, world!'
puts ​''
puts ​'Good-bye.' ...

Get Learn to Program, 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.