Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

integer
size
ruby
communitycreator

What is the integer.size method in Ruby?

okwudili onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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}"
Implementing the "integer.size" method

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.

RELATED TAGS

integer
size
ruby
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring