Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
communitycreator

What is the integer.divmod() 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

In Ruby, the divmod() method is used on an integer number value to get an array. This array contains the quotientThe quotient is the division of operand one by operand two, which is then rounded using the floor() method. For example, floor(4/2). and remainderThe remainder is the modulus of operand one and operand two. For example, mod(4,2). of the integer. For example, [2, 0], where 2 is the quotient and 0 is the remainder.

Syntax

int1.divmod(int2)

Parameters

This method takes a mandatory parameter, int2, which represents an integer.

Return value

This method returns an array that contains the quotient and the remainder of the integer value.

Code example

# create some integers
int1 = 45
int2 = 4*3-3+5
int3 = 4**3
int4 = -5
# get bit length
puts "#{int1.divmod(5)}"
puts "#{int1.divmod(3)}"
puts "#{int1.divmod(7)}"
puts "#{int1.divmod(1)}"
Using the divmod() method on an integer in Ruby

Explanation

  • Lines 2–5: We create some integers and initialize them with some values.
  • Lines 8–11: We invoke the divmod() method on the integer values and pass some arguments. Next, we print the output to the console.

RELATED TAGS

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