Ruby Integer magnitude function with example

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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  

chevron_right


Output:

21
21
0
100

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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   

chevron_right


Output:

29
7
90
10


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.