Ruby Integer size() function with example

The size() function in Ruby returns the number of bytes in the machine representation of int.

Syntax: number.size

Parameter: The function takes the integer whose number of bytes is returned.



Return Value: The function returns the number of bytes.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for size() function  
   
# Initializing the numbers 
num1 = 18
num2 = 2**1000
num3 = 2**19
num4 = 5**100
   
      
# Printing the size value
puts num1.size
puts num2.size
puts num3.size
puts num4.size

chevron_right


Output:

8
126
8
30

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for size() function  
   
# Initializing the numbers 
num1 = 18
num2 = 5**1000
num3 = 19**19
num4 = 12**100
   
      
# Printing the number 
# bytes used 
puts num1.size
puts num2.size
puts num3.size
puts num4.size

chevron_right


Output:

8
291
11
45


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.