Strings and numbers are the bread and butter of literals. Ruby provides support for both integers and floating-point numbers, using classes Fixnum, Bignum, and Float.


Integers are instances of class Fixnum or Bignum:

123                    # decimal
1_234                  # decimal with underline 
0377                   # octal
0xff                   # hexadecimal
0b1011                 # binary
?a                     # character code for 'a'
12345678901234567890   # Bignum:  an integer of infinite length

Floating-point numbers

Floating-point numbers are instances of class Float:

123.4                    # floating point value
1.0e6                    # scientific notation
4E20                     # dot not required
4e+20                    # sign before exponential

Get Ruby in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.