Ruby odd? Method

Ruby odd? Function with Examples

Ruby odd? method is an integer class function that is used to check the specified number whether it is odd or not. Applying this odd method to other data types rather than integer will raise an error.

Ruby odd? Method

Syntaxnum.odd?
Where num is an integer number.
ParametersThis method does not accept any parameters.
Return valueReturns boolean values either true on false depending on the specified number. If the number is odd then return true otherwise false.

Example Program-1:

num1 = 11
num2 = 26
num3 = -14

x = num1.odd?
y = (num2).odd?
z = num3.odd?
m = 13.odd?

puts x
puts y
puts z
puts m

Output

true
false
false
true

Example Program-2:

num1 = 11
num2 = 26
num3 = -14

puts num1.odd?
puts num2.odd?
puts num3.odd?
puts 9.odd?

Output

true
false
false
true
Posted in