Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is Modulo in Ruby?

In this blog post, we will explore the concept of modulo in the Ruby programming language. If you are just starting to learn programming, you might be wondering what modulo is and why it matters. Don't worry, we will go step by step and try to avoid using jargons. And if we do use some jargons, we'll make sure to explain them.

So, without further ado, let's dive into the world of modulo in Ruby.

Understanding Modulo

Before we dive into the specifics of modulo in Ruby, let's first understand what modulo is. Modulo, often represented as the percentage sign (%), is an arithmetic operation that returns the remainder after division. In other words, it tells you what's left over after you've divided one number by another.

For example, let's say you have 7 apples and you want to divide them equally among 3 people. Each person would get 2 apples, but after dividing the apples, you would still have 1 apple left over. In this case, the modulo operation would tell you that there is a remainder of 1.

Here's the same example in mathematical terms: 7 (dividend) % 3 (divisor) = 1 (remainder)

Modulo in Ruby

In Ruby, the modulo operation can be performed using the % operator. Let's take a look at some examples of modulo in action:

remainder = 7 % 3
puts remainder #=> 1

In the above example, we used the modulo operator to calculate the remainder after dividing 7 by 3, which is 1.

Let's take a look at another example:

remainder = 10 % 4
puts remainder #=> 2

Here, we used the modulo operator to calculate the remainder after dividing 10 by 4, which is 2.

Now that we have a basic understanding of the modulo operation and how to use it in Ruby, let's explore some practical applications and use cases.

Practical Applications of Modulo

1. Checking for Even and Odd Numbers

One common use case for the modulo operation is to check if a number is even or odd. If a number is evenly divisible by 2, it's an even number. Otherwise, it's an odd number. By using the modulo operation, we can easily check this:

def is_even?(number)
  number % 2 == 0
end

def is_odd?(number)
  number % 2 != 0
end

puts is_even?(4) #=> true
puts is_odd?(7)  #=> true

In the above example, we used modulo to check if a number is even or odd by checking if the remainder after dividing the number by 2 is 0 or not.

2. Performing Clock Arithmetic

Another practical application of modulo is performing clock arithmetic. Clock arithmetic is a way of calculating the time after a certain number of hours, minutes, or seconds have elapsed. For example, if the current time is 8:00 and 5 hours have passed, the new time would be 1:00.

We can use the modulo operation to perform clock arithmetic as follows:

def add_hours(current_hour, hours_to_add)
  (current_hour + hours_to_add) % 12
end

new_hour = add_hours(8, 5)
puts new_hour #=> 1

In this example, we used modulo to calculate the new hour by adding the current hour and the number of hours to add, and then taking the remainder after dividing the result by 12 (since there are 12 hours on a clock).

3. Cycling Through Arrays

Modulo can also be used to cycle through arrays. Suppose you have an array of items and you want to loop through the items repeatedly, modulo can help you achieve this.

colors = ["red", "green", "blue"]

def next_color(colors, current_index)
  colors[(current_index + 1) % colors.length]
end

current_index = 0
3.times do
  current_index += 1
  puts next_color(colors, current_index) #=> "green", "blue", "red"
end

In this example, we used modulo to calculate the index of the next item in the array. By taking the remainder after dividing the current index plus one by the length of the array, we can ensure that the index always stays within the bounds of the array and cycles back to the beginning when it reaches the end.

Modulo and Integer Division

In Ruby, there are two division operators: / and div. The / operator performs floating-point division, while the div operator performs integer division, which discards the remainder and returns the largest integer less than or equal to the result of the division.

Modulo can be used in conjunction with integer division to get both the quotient and the remainder of a division.

dividend = 13
divisor = 4

quotient = dividend.div(divisor) #=> 3
remainder = dividend % divisor   #=> 1

puts "13 divided by 4 is 3 with a remainder of 1"

In this example, we used the div operator to perform integer division and the % operator to calculate the remainder.

Conclusion

In this blog post, we explored the concept of modulo in Ruby and its practical applications. We learned that modulo is an arithmetic operation that returns the remainder after division, and we can use the % operator in Ruby to perform modulo calculations.

We also saw how modulo can be used for various tasks, such as checking for even and odd numbers, performing clock arithmetic, and cycling through arrays.

Understanding and using modulo in your Ruby programs can help you solve various problems more efficiently and elegantly. Keep practicing, and you'll soon find even more ways to use modulo in your code. Happy coding!