Open In App

Ruby Integer magnitude function with example

Last Updated : 07 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The magnitude() function in Ruby returns the absolute value of the integer. It is similar to the abs function and is an alias of it.

Syntax: (number).magnitude

Parameter: The function takes the integer whose magnitude is to be returned.

Return Value: The function returns the absolute value of the integer.

Example #1:




# Ruby program of Integer magnitude function
  
# Initializing the numbers 
num1 = -21
num2 = 21 
num3 = 0 
num4 = -100 
  
# Printing the magnitude   value of integers 
puts (num1).magnitude  
puts (num2).magnitude  
puts (num3).magnitude  
puts (num4).magnitude  


Output:

21
21
0
100

Example #2:




# Ruby program of Integer magnitude function
  
# Initializing the numbers 
num1 =29
num2 = -7
num3 = 90
num4 = -10
   
      
# Printing the magnitude value of integers 
puts (num1).magnitude  
puts (num2).magnitude  
puts (num3).magnitude  
puts (num4).magnitude   


Output:

29
7
90
10

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads