Table of contents
1.
Introduction
2.
Arithmetic Operators in Ruby
3.
Addition(+)
3.1.
Example
3.1.1.
Output
4.
Subtraction(-)
4.1.
Example
4.1.1.
Output
5.
Multiplication(*)
5.1.
Example
5.1.1.
Output
6.
Division(/)
6.1.
Example
6.1.1.
Output
7.
Modulus(%)
7.1.
Example
7.1.1.
Output
8.
Frequently Asked Questions
8.1.
What does Ruby's |= stand for?
8.2.
In Ruby, what is a lambda?
8.3.
In Ruby, what is self?
8.4.
In Ruby, what is yield?
9.
Conclusion
Last Updated: Mar 27, 2024

Arithmetic Operations in Ruby

Author SHIVANGI MALL
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Ruby is an open-source, object-oriented scripting language. Yukihiro Matsumoto created Ruby in the mid-1990s. Ruby is an excellent language for making desktop programs, static webpages, data processing services, and even automation solutions. Web servers, DevOps, and web scraping and crawling are all examples of where it's employed. When you bring in the Rails application framework's features, you can accomplish even more, especially with database-driven web apps.

In this article, we will discuss some arithmetic: +, –, *, /, and % operations on Ruby to enable you to go to higher levels of expertise. Before reading this article, you need to have a basic understanding of computer programming terms and ruby.  If this isn't the case, come back here after reading our Ruby article.

Arithmetic Operators in Ruby

These operators are mathematical operators that can be used to perform simple or complex arithmetic operations on primitive data types as operands. These are a set of unary and binary operators that can be applied to one or two operands. Let's have a look at the various arithmetic operators available in Ruby.

Addition(+)

Adds values on either side of the operator.

Example

puts ("Addition of 5 and 6 is") 
puts(5 + 6) 
puts ("Addition of 11 and 2 is") 
puts(11 + 2) 
You can also try this code with Online Ruby Compiler
Run Code

Output

Addition of 5 and 6 is
11
Addition of 11 and 2 is
13
You can also try this code with Online Ruby Compiler
Run Code

Subtraction(-)

Subtracts right hand operand from left-hand operand.

Example

puts ("Subtraction of 11 and 2 is") 
puts(11 -  2) 
puts ("Subtraction of 8 and 4 is") 
puts(8 - 4) 
You can also try this code with Online Ruby Compiler
Run Code

Output

Subtraction of 11 and 2 is
9
Subtraction of 8 and 4 is
4
You can also try this code with Online Ruby Compiler
Run Code

Multiplication(*)

Multiplies operands value on either side of the operator.

Example

puts ("Multiplication of 11 and 2 is") 
puts(11 *  2) 
puts ("Multiplication of 8 and 4 is") 
puts(8 * 4) 
You can also try this code with Online Ruby Compiler
Run Code

Output

Multiplication of 11 and 2 is
22
Multiplication of 8 and 4 is
32
You can also try this code with Online Ruby Compiler
Run Code

Division(/)

Divides left hand operand by right hand operand.

Example

puts ("Division of 12 and 2 is") 
puts(12/ 2) 
puts ("Division of 8 and 4 is") 
puts(8 / 4) 
You can also try this code with Online Ruby Compiler
Run Code

Output

Division of 12 and 2 is
6
Division of 8 and 4 is
2
You can also try this code with Online Ruby Compiler
Run Code

Modulus(%)

Divides left hand operand by right hand operand and return the remainder.

Example

puts ("Modulus of 11 and 2 is") 
puts(11 %  2) 
puts ("Modulus of 8 and 3 is") 
puts(8 % 3) 
You can also try this code with Online Ruby Compiler
Run Code

Output

Modulus of 11 and 2 is
1
Modulus of 8 and 3 is
2
You can also try this code with Online Ruby Compiler
Run Code

Frequently Asked Questions

What does Ruby's |= stand for?

It's short for or-equals-to. It first checks whether the value on the left is defined, after which it uses it. If it isn't, use the right-hand value.

In Ruby, what is a lambda?

A lambda is a Ruby object that is comparable to a proc. A lambda, unlike a proc, requires a fixed number of arguments to be supplied to it, and instead of returning immediately, it returns s to its calling procedure.

In Ruby, what is self?

self is a unique variable that refers to the object that "owns" the code that is now running.

In Ruby, what is yield?

Yield is a Ruby keyword that allows a developer to give an argument to a block. The amount of arguments that can be passed to the block is unlimited.

Conclusion

We've covered a lot of topics in this post about arithmetic operators and how they're implemented in  Ruby. We looked at addition, subtraction, multiplication, division, and modulus operators in depth using examples.

We hope this article helps you to learn something new. And if you're interested in learning more, see our posts on 8 reasons why Ruby should be your first language!RubyIntroduction to Ruby on RailsRuby on rails for your next web development project!Ruby on Rails.

To learn more about Micro Operations, refer to Arithmetic Micro Operations.

Visit our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Thank you for reading this post :-)

Feel free to upvote and share this article if it has been useful for you.

Live masterclass