In Ruby, we use .pred
to return the immediate predecessor of a number. When it is called on a number, it returns that number minus 1.
number.pred
The pred
function takes no parameters. It only needs to be called on a number value.
In the code below, the pred
function is called on some number values and the returned values are printed on the console.
# create numbersnum1 = 1num2 = 34num3 = 4500 # same as 4_500num4 = 732394num5 = -2num6 = -23num7 = -100# print value to the consoleputs num1.predputs num2.predputs num3.predputs num4.predputs num5.predputs num6.predputs num7.pred
Note: When the
pred
function is called on afloat
instead of aninteger
, an error will be thrown.
# create a float numberfloatNumber = 0.5# print `pred` value to consoleputs floatNumber.pred # will throw an error.