Ruby Integer pred() function with example
The pred function in Ruby returns the immediate predecessor of the number, i.e., it returns number – 1. If a float value is used, it throws an error message.
Syntax: number.pred
Parameter: The function takes the integer whose predecessor is to be returned.
Return Value: The function returns the immediate predecessor of the number, i.e., it returns number – 1
Example 1:
# Ruby program for pred function # Initializing the numbers num1 = 100 num2 = 17 num3 = - 90 num4 = - 29 # Printing the pred value puts num1.pred puts num2.pred puts num3.pred puts num4.pred |
Output:
99 16 -91 -30
Example 2:
# Ruby program for pred function # Initializing the numbers num1 = 19 num2 = - 17 num3 = - 18 num4 = 16 # Printing the pred value puts num1.pred puts num2.pred puts num3.pred puts num4.pred |
Output:
18 -18 -19 15
Contact Us