Integer Type
An Integer is a whole number, and Ruby can hold numbers as big as you like. It is like counting apples with no upper limit.
In this page:
Syntax
number = 42
big = 10_000_000
number.class # Integer
Integer Type
Ruby's Integer type represents whole numbers of arbitrary size -- there's no fixed-width overflow like in many other languages, since Ruby automatically promotes to arbitrary precision as needed.
Integer division truncates toward negative infinity. Every integer is an object, so you can call methods directly on a literal, like 5.times.
Note:
Because integers are objects, you can call methods directly on a literal, like
3.even? or 5.times { ... }.Example: Integer Type
x = 42
big = 123456789012345678901234567890
puts x.class
puts big
puts 7 / 2
puts 5.even?
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Dividing integers with
/and expecting decimals, when7 / 2is3; use7 / 2.0. - Worrying about integer overflow, when Ruby integers grow as needed.
- Writing
5.evenwithout the?, when the method iseven?.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: