What is the magnitude() method of a float in Ruby?
Overview
The magnitude() method of a float is used to convert a float value to its absolute form.
Syntax
float_value.magnitude()
Parameters
This methodis only invoked on the float value.
Return value
This method returns the absolute value of the float_value.
Code
# create some float valuesf1 = 2.222f2 = 3.4/-0.222f3 = -(3 + 3) * 2f4 = 20.92# print magnitudesputs f1.magnitudeputs f2.magnitudeputs f3.magnitudeputs f4.magnitude
Code explanation
-
Lines 2–5: We create four variables
f1,f2,f3, andf4that contain different float values. -
Lines 8–11: We invoke the
magnitude()method on these float values and print the returned results to the console, using theputsmethod.