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 values
int1 = 10
int2 = 256**20 + 1
int3 = 256**10 - 1
int4 = 300 **4 - 3
# get size
puts "#{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 size property to get the number of bytes of the integers. And then, we print the results to the console.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources