Numbers in Ruby

Let's start with numbers, which fall into two general categories: those that can have a fractional part (floats) and those that cannot (integers).

Integer Literals

A literal is just a way of expressing something directly in a language, as opposed to using a variable. Writing integer literals is intuitively pretty simple—we've been doing it all along—but Ruby offers some enhancements that help you express yourself readably.

A literal like 863527117 isn't very readable, because you have to look closely and count digits to know its magnitude. Is that in the hundreds of millions, or is it in the billions? When you aren't programming a computer, the convention is to break large numbers up with commas. Saying “863,527,117” makes it ...

Get Sams Teach Yourself Ruby in 21 Days 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.