What is number.integer? in Ruby?

The integer? method is a method that checks if a number is an integer.

In Ruby, numbers are either integers or floats. Numbers without decimal points are called integers. For example:

  • 2,5
  • 100
  • 180000
  • 348888787
  • 2_000

While numbers with decimal points are called floating-point numbers or floats. For example:

  • 1.3
  • 3.0
  • 399.99

Note: In Ruby, underscores _ can be used to separate a thousand places e.g., 2500 is the same as 2_500

If a number is truly an integer, the integer? method returns true. If it is not an integer, it returns false.

Syntax

number.integer?

Parameter

This method takes no parameters.

Return value

The method returns true if a number is an integer. Otherwise, it returns false.

Code

In the code below, we call the integer? methods on certain numbers and print the values returned to the console.

# create numbers
num1 = 1
num2 = 200
num3 = 9.0
num4 = 12.23
num5 = 3.142 # Pi value in maths
num6 = 12999
num7 = 12_999 # same as 12999
# call the integer? method
# and print values
puts num1.integer? # true
puts num2.integer? # true
puts num3.integer? # false
puts num4.integer? # false
puts num5.integer? # false
puts num6.integer? # true
puts num7.integer? # true
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources