A quick reminder that number literals in Ruby can be pretty fancy!
Example | Evaluates To | Class | Purpose |
---|---|---|---|
0x10 |
16 |
Integer | Integers in hexadecimal (0-16) format |
0o10 ¹ |
8 |
Integer | Integers in octal (0-8) format |
0b10 |
2 |
Integer | Integers in binary (0-1) format |
1e1000 |
Float::INFINITY |
Float | Floats in exponential notation |
1i |
(0+1i) ² |
Complex | Shorthand for creating complex numbers |
3/6r |
(1/2) ² |
Rational | Shorthand for creating rational numbers |
0_0 |
0 |
any | Visually separate digits |
¹ Also: 010
² While the representation of a complex number (e.g. (0+1i)
) is a valid way to create the same number again, this is not true for (1/2)
which will just evaluate to 0
³. Also note that the r
only makes the 6
a rational, which in turn "rationalizes" the result. An equivalent way of expressing the same fraction would be 3r/6
³ Except when you require "mathn"
More Idiosyncratic Ruby
- Please Comment on GitHub
- Next Article: Symbolic Validations
- Previous Article: Sad Methods