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:
While numbers with decimal points are called floating-point numbers or floats. For example:
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
.
number.integer?
This method takes no parameters.
The method returns true
if a number is an integer. Otherwise, it returns false
.
In the code below, we call the integer?
methods on certain numbers and print the values returned to the console.
# create numbersnum1 = 1num2 = 200num3 = 9.0num4 = 12.23num5 = 3.142 # Pi value in mathsnum6 = 12999num7 = 12_999 # same as 12999# call the integer? method# and print valuesputs num1.integer? # trueputs num2.integer? # trueputs num3.integer? # falseputs num4.integer? # falseputs num5.integer? # falseputs num6.integer? # trueputs num7.integer? # true