← Back to Ruby Course | Chapter 2: Variables & Types | Lesson 2 of 7

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:

  1. Integer Type
Syntax
ruby
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

markup
x = 42
big = 123456789012345678901234567890

puts x.class
puts big
puts 7 / 2
puts 5.even?
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
  1. Dividing integers with / and expecting decimals, when 7 / 2 is 3; use 7 / 2.0.
  2. Worrying about integer overflow, when Ruby integers grow as needed.
  3. Writing 5.even without the ?, when the method is even?.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.