Ruby integer? Method

Ruby integer? Function with Examples

Ruby integer? method determines whether a specified number is an integer or not. If the specified number is integer returns true otherwise false.

This method can check between two data types; integer and float/double. Calling other data types will raise an error.

Ruby integer? Method

Syntaxnum.integer?
Where num is the value to check whether the value is an integer or not.
ParametersThis method does not accept any parameters but takes an integer or double number as an object.
Return valueReturns boolean values either true or false depending on the value. If the value is integer returns true otherwise returns false.

Example Program-1:

num1 = 10
num2 = 10.20
x = num1.integer?
y = num2.integer?
z = (34.20).integer?

puts x
puts y
puts z

Output

true
false
false

Example Program-2:

num1 = 13.3
num2 = -12
num3 = 0
num4 = 32.5

puts num1.integer?
puts num2.integer?
puts (num3).integer?
puts (num4).integer?
puts 7.integer?

Output

false
true
true
false
true
Posted in