Skip to Main Content
Learn to Program, 2nd Edition
book

Learn to Program, 2nd Edition

by Chris Pine
March 2009
Intermediate to advanced content levelIntermediate to advanced
194 pages
4h
English
Pragmatic Bookshelf
Content preview from Learn to Program, 2nd Edition

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.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learn to Program, 3rd Edition

Learn to Program, 3rd Edition

Chris Pine
The Book of Ruby

The Book of Ruby

Huw Collingbourne

Publisher Resources

ISBN: 9781680500172Errata