02. Letters
So, 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 letters printed together along a banner). To make it easier to see which parts of the code are strings, I'll color the strings red. Here are some examples of strings:
'Hello.'
'Ruby rocks.'
'5 is my favorite number... what is yours?'
'Snoopy says #%^?&*@! when someone steps on his toe.'
' '
''
As you can see, strings can contain punctuation, digits, symbols, and spaces... much more than just letters. The last string has nothing in it; we call that an empty string.
We've been using puts to print numbers; let's try it again with some strings:
puts 'Hello, world!'
puts ''
puts 'Good-bye.'
Hello, world!
Good-bye.
That worked well. Now try some strings yourself.
String Arithmetic
Just as you can do arithmetic on numbers, you can also do arithmetic on strings! Well, part of it... you can add strings, anyway. Let's try to add two strings and see what puts does.
puts 'I like' + 'apple pie.'
I likeapple pie.
Whoops! I forgot to put a space between 'I like' and 'apple pie.'. Spaces don't usually matter in your code, but they matter inside strings. (It's true what they say: computers don't do what you want them to do, only what you tell them to do). Let's try again:
puts 'I like ' + 'apple pie.'
puts 'I like' + ' apple pie.'
I like apple pie.
I like apple pie.
(As you can see, it doesn't matter which string I add the space to.)
So you can add strings. But you can also multiply them! (By a number, anyway). Watch this:
puts 'blink ' * 4
batting her eyes
(Just kidding... it actually does this:)
blink blink blink blink
If you think about it, this makes perfect sense. After all, 7*3 really means 7+7+7, so 'moo'*3 just means 'moo'+'moo'+'moo'.
12 vs '12'
Before we go any further, we need to make sure we understand the difference between numbers and digits. 12 is a number, but '12' is a string of two digits.
Let's play around with this for a bit:
puts 12 + 12
puts '12' + '12'
puts '12 + 12'
24
1212
12 + 12
How about this?
puts 2 * 5
puts '2' * 5
puts '2 * 5'
10
22222
2 * 5
These examples were pretty straightforward. However, if you're not careful when mixing strings and numbers, you might run into...
Problems
At this point, you may have tried some things that didn't work. If not, here are a few:
puts '12' + 12
puts '2' * '5'
#<TypeError: can't convert Fixnum into String>
Hmmm... An error message. The problem is that you can't really add a number to a string, or multiply a string by another string. It doesn't make much sense, just like:
puts 'Betty' + 12
puts 'Fred' * 'John'
One thing you should know: you can write 'pig'*5 in a program, since that just means 5 sets of the string 'pig' all added together. However, you cannot write 5*'pig', since that means 'pig' sets of the number 5, which is just silly.
The Apostrophe (Escape)
Finally, what about a program that prints You're swell!? We can try this:
puts 'You're swell!'
Well, that won't work; I won't even try to run it. The computer thought we were done with the string. (This is why it's good to have a text editor that does syntax highlighting for you). So how do we let the computer know we want to stay in the string? We have to escape the apostrophe, like this:
puts 'You\'re swell!'
You're swell!
The backslash (\) is the escape character. In other words, if you have a backslash and another character, they are sometimes translated into a new character. The only things the backslash escapes, though, are the apostrophe and the backslash itself. (If you think about it, escape characters must always escape themselves).
I think a few examples are in order:
puts 'You\'re swell!'
puts 'backslash at the end of string: \\'
puts 'up\\down'
puts 'up\down'
You're swell!
backslash at the end of string: \
up\down
up\down
Since the backslash does not escape 'd', but does escape itself, the last two strings are identical. They don't look the same in the code, but in your computer they are the same.
If you have any other questions, just keep reading! I can't answer every question on this page.