Chapter 5Mixing It Up

We’ve looked at a few kinds of objects (integers, floats, and strings), and we made variables point to them. Now it’s time for them all to play nicely together.

We’ve seen that if we want a program to print 25, the following does not work, because you can’t add numbers and strings together:

var1 = 2
var2 = ​'5'
puts var1 + var2

Part of the problem is that your computer doesn’t know if you were trying to get 7 (2 + 5) or if you wanted to get 25 ('2' + '5'). But we’ll learn how to do both.

Before we can add these together, we need some way of getting the string version of var1 or of getting the integer version of var2.

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.