Integer is Ruby’s built-in class for representing whole-number values. Integers are objects: they have classes, receive messages, and belong to Ruby’s numeric class family beneath Numeric. section 8.3 section 8.3.1
Ruby integers support ordinary arithmetic and several representation rules:
- Integer division returns an integer: 16 / 5 evaluates to 3; use a floating-point operand for floating-point division. section 8.3.2
- Modulo returns a remainder, so 10 % 3 evaluates to 1. section 8.3.2
- Prefixes select bases: 0x introduces hexadecimal, while a leading 0 introduces octal. section 8.3.2
- Arithmetic operators are methods under Ruby’s operator syntax, as shown by 1.+(1) and 12./(3). section 8.3.2
Integer arithmetic in a temperature converter
A small Ruby program can keep its input and output as integers: celsius = 100; fahrenheit = (celsius * 9 / 5) + 32. The calculation demonstrates integer arithmetic inside a runnable program. section 1.1.5
Table 8.1 Common arithmetic expressions and their evaluative results
| Expression |
Result |
Comments |
|---|---|---|
|
|
2 |
Addition |
|
|
2 |
Integer division |
|
|
3 |
Integer division (no automatic floating-point conversion) |
|
|
3.0303030303 |
Floating-point division |
|
|
4.6 |
Floating-point addition |
|
|
–5 |
Subtraction |
|
|
1 |
Modulo (remainder) |
Compare integer division, floating-point division, subtraction, addition, and modulo through compact evaluated examples. section 8.3.2














