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 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