In Ruby, divmod()
is a float method used on a number value to get an array. This array contains the quotient and remainder of the number value.
The quotient is the division of operand one by operand two, which is then rounded using the floor()
method. For example, floor(4/2)
.
The remainder is the modulus of operand one and operand two. For example, mod(4,2)
.
operand_one.divmod(operand_two)
This method takes operand_two
as a parameter. It’s a number value. It’s mandatory.
This method returns an array. This array contains the quotient and remainder of operand_one
and operand_two
.
# create some float valuesf1 = 34.344f2 = 2.423f3 = -5/2.3# invoke the coerce methodputs "#{f1.divmod(1)}"puts "#{f2.divmod(2)}"puts "#{f3.divmod(3)}"
f1
, f2
, and f3
.divmod()
method on the float values. Next, we print the results.