12 vs. ’12’

Before we get any further, we should 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 while:

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 are pretty clear. However, if you’re not too careful with how you mix your strings and your numbers, you might run into…

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.