What is the integer.size method in Ruby?
Overview
The size property of an integer is used to get the number of bytes in the machine representation of int (machine-dependent).
Syntax
integer.size
Return value
The value returned is an integer, which represents the number of bytes in the machine representation of an integer.
Code example
# create integer valuesint1 = 10int2 = 256**20 + 1int3 = 256**10 - 1int4 = 300 **4 - 3# get sizeputs "#{int1.size}"puts "#{int2.size}"puts "#{int3.size}"puts "#{int4.size}"
Code explanation
- Lines 2–5: We create some integer values.
- Lines 8–11: We use the
sizeproperty to get the number of bytes of the integers. And then, we print the results to the console.