Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

float
ruby
communitycreator

What is the magnitude() method of a float in Ruby?

okwudili onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 values
f1 = 2.222
f2 = 3.4/-0.222
f3 = -(3 + 3) * 2
f4 = 20.92
# print magnitudes
puts f1.magnitude
puts f2.magnitude
puts f3.magnitude
puts f4.magnitude
Get the absolute values of float values with the "magnitude()" method

Code explanation

  • Lines 2–5: We create four variables f1, f2, f3, and f4 that 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 the puts method.

RELATED TAGS

float
ruby
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring