Ruby Integer prime? function with example
The prime? function in Ruby returns a boolean value. It returns true if the number is prime, else it returns false. It requires the ‘prime’ class to be pre-added.
Syntax: number.prime?
Parameter: The function takes the integer which is to be checked for prime or not.
Return Value: The function returns a boolean value which determines if the value is prime or not.
Example 1:
Ruby
# Ruby program for prime? function # Pre-defined classrequire 'prime' # Initializing the numbersnum1 = 100num2 = 17num3 = 90num4 = 29 # Printing if prime or notputs num1.prime?puts num2.prime?puts num3.prime?puts num4.prime? |
Output:
false true false true
Example 2:
Ruby
# Ruby program for prime? function # Pre-defined classrequire 'prime' # Initializing the numbersnum1 = 13num2 = 19num3 = 18num4 = 10 # Printing if prime or notputs num1.prime?puts num2.prime?puts num3.prime?puts num4.prime? |
Output:
true true false false


