Numbers

Ruby supports integers, floating-point, rational, and complex numbers. Integers can be of any length (up to a maximum determined by the amount of free memory on your system) and are of type Integer.

Integers are assumed to be decimal base 10, but, you can write integers using a leading sign as an optional base indicator—0 for octal, 0x for hex, or 0b for binary (and 0d for decimal)—followed by a string of digits in the appropriate base.

Underscore characters are ignored in the digit string, you’ll see them used in place of commas in larger numbers.

 123456 => 123456 ​# base 10
 0d123456 => 123456 ​# base 10
 123_456 => 123456 ​# underscore ignored
 -543 => -543 ​# negative number
 0xaabb => 43707 ​# hexadecimal
 0377 => 255 ​ ...

Get Programming Ruby 3.3 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.