Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
math
magnitude
communitycreator

What is the magnitude() function in Ruby?

Harris Amjad

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.

The magnitude() function in Ruby returns the absolute valuenon-negative value of any real number. of a given number.

Figure 1 below shows the visual representation of the magnitude() function.

Figure 1: Visual representation of magnitude() function

Syntax

num.magnitude()
# where num is the number whose magnitude is to be calculated

Parameter

The num.magnitude() function does not require any parameter.

Return value

This function returns the magnitude or absolute value of the given number.

Code

The following example shows how to use the magnitude() function in Ruby.

#positive number
print "(-30).magnitude() : ",(-30).magnitude(), "\n"
#negative number
print "(20).magnitude(): ",(20).magnitude(), "\n"
#zero
print "(0).magnitude(): ",(0).magnitude(), "\n"
#fractional number
print "(-1.24).magnitude(): ",(-1.24).magnitude(), "\n"

RELATED TAGS

ruby
math
magnitude
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