Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Expression 
3.
Operators
4.
Types of operators in Ruby
4.1.
Arithmetic Operators in Ruby
4.2.
Comparison Operators in Ruby
4.3.
Assignment Operators in Ruby
4.4.
Parallel Assignment in Ruby
4.5.
Bitwise Operators in Ruby
4.6.
Logical Operators in Ruby
4.7.
Ternary Operator in Ruby
4.8.
Range Operators in Ruby
5.
Frequently Asked Questions
5.1.
What is Ruby?
5.2.
What are operators in Ruby?
5.3.
What is the language in which is Ruby written?
5.4.
Is Ruby a backend Language? 
5.5.
Can Ruby be used for Web development?
6.
Conclusion
Last Updated: Mar 27, 2024

Introduction to Expressions and Operators in Ruby

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Expressions and operators in Ruby are different terms. An expression is a piece of Ruby code known that can be evaluated by the Ruby interpreter to yield a value. Literals, variable references, and method calls are examples of primary expressions that may be combined into more complex expressions using operators. Expressions and operators in Ruby are basic building blocks of a program. 

In the following article, we will dive deeper into the expressions and operators in Ruby language.

Expression 

Operands and operators are used to build expressions. An expression's operators specify which operations should be applied to the operands. The precedence and associativity of the operators govern the order in which they are evaluated in an expression.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Operators

Operators, in expressions and operators in Ruby language, are particular symbols that denote the execution of a specific procedure. Mathematical operators in Ruby are used in programming languages. Programmers work with data that is processed using the operators. An operand is one of an operator's inputs (arguments).

Types of operators in Ruby

The following is the list of different types of operators in the Ruby language.

Arithmetic Operators in Ruby

Operator

Description

+ Addition − Adds values on either side of the operator.
- Subtraction − Subtracts the right-hand operand from the left-hand operand.
* Multiplication − Multiplies values on either side of the operator.
/ Division − Divides left-hand operand by right-hand operand.
% Modulus − Divides left-hand operand by right-hand operand and returns the remainder.
** Exponent − Performs exponential (power) calculation on operators.

Comparison Operators in Ruby

Operator

Description

== Checks whether the value of two operands is equal or not. If yes, then the condition becomes true.
!= Checks whether the value of two operands is equal or not. If values are not equal, then the condition becomes true.
> Checks if the value of the left operand is greater than the value of the right operand. If yes, then the condition becomes true.
< This operator checks if the value of the left operand is greater than the right operand. If yes, then the condition becomes true.
>= Checks whether the value of the left operand is greater than or equal to the right operand and returns true if the condition is fulfilled.
<= Checks whether the value of the left operand is less than or equal to the right operand and returns true if the condition is fulfilled.
<=> Combined comparison operator. Returns 0 if the first operand equals the second, 1 if the first operand is greater than the second, and -1 if the first operand is less than the second.
=== Used to test equality within a when clause of a case statement.
.eql? True if the receiver and argument have both the same type and equal values.
equal? True if the receiver and argument have the same object id.

Assignment Operators in Ruby

Operator

Description

= Simple assignment operator, that assigns values from right side operands to the operand on the left side.
+= Add AND assignment operator; it adds the right operand to the left operand and assigns the result to the left operand.
-= Subtract AND assignment operator; subtracts the right operand from the left operand and assigns the result to the left operand.
*= Multiply AND assignment operator; multiplies right operand with the left operand, and assigns the result to the left operand.
/= Divide AND assignment operator; divides the left operand with the right operand and assigns the result to the left operand.
%= Modulus AND assignment operator; takes modulus using two operands and assigns the result to the left operand.
**= Exponent AND assignment operator, Performs exponential (power) calculation on operators and assigns value to the left operand.

Parallel Assignment in Ruby

Ruby also allows us to assign variables in parallel. This allows us to initialize numerous variables with just one line of Ruby code. For instance,

P, Q, R = 10,20,30

Parallel assignment can also be used to swap values between two variables.

P, Q = Q, R

Bitwise Operators in Ruby

Operator

Description

& Binary AND operator put a bit to the result if it exists in both operands.
| Binary OR operator put a bit if it exists in either operand.
^ Binary XOR Operator puts the bit if it is set in one operand but not both.
~ Binary Ones Complement Operator is unary and flips the present bits.
<< Binary Left Shift Operator. The left operand value is moved towards the left by the number of bits specified by the right operand.
>> Binary Right Shift Operator. The left operand value is moved right by the number of bits specified by the right operand.

Logical Operators in Ruby

Operator

Description

and Called Logical AND operator. If both the operands hold true, then the condition becomes true.
or Called Logical OR Operator. If any of the two operands hold true, then the condition becomes true.
&& Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.
|| Called Logical OR Operator. The condition becomes true if any of the two operands are non-zero.
! Called Logical NOT Operator. Reverses the logical state of the operand it is applied on.
not Called Logical NOT Operator. Use to reverse the logical state of its operand. If a condition is true, then the Logical NOT operator will make false.

Ternary Operator in Ruby

Another operator is known as Ternary Operator. It evaluates an expression for a true or false value before executing one of the two specified statements based on the evaluation result. Given below is the syntax for the conditional operator:

a>b? a:b


Where a and b are user-defined variables.

Range Operators in Ruby

In Ruby, sequence ranges are used to produce a set of values that include a start value, an end value, and a range of values in between.

Ruby's ".." and "..." range operators are used to produce these sequences. The two-dot form creates a range that includes the provided high number, but the three-dot form creates a range that excludes it.

Operator

Description

.. Creates a range from start point to end point inclusive.
Creates a range from the start point to the exclusive endpoint.

 

Read about Bitwise Operators in C here.

Frequently Asked Questions

What is Ruby?

Ruby is a multi-purpose programming language that may be used to accomplish many tasks. Ruby is a powerful programming language for constructing desktop applications, static websites, data processing services, and even automation solutions.

What are operators in Ruby?

A symbol denoting an operation performed with one or more operands is called an operator. Any programming language's base is made up of operators. We can use operators to execute a variety of operations on operands.

What is the language in which is Ruby written?

Ruby is written in C.  

Is Ruby a backend Language? 

Yes, Ruby can be used as a backend language.

Can Ruby be used for Web development?

According to Slant, Ruby on Rails is the fifth most popular framework for backend development. The framework has been used to build over 350,000 websites worldwide, and this number is continually increasing.

Conclusion

In this article, we have extensively discussed expressions and operators in Ruby. We began with a brief introduction to expressions and operators in Ruby, followed by a list of all its operators. 

After reading about the expressions and operators in Ruby, are you not feeling excited to read/explore more articles on the Ruby programming language? Don't worry; Coding Ninjas has you covered.  To learn, see RubyRuby coding NinjasDocumentationOfficial Ruby FAQ, and Ruby Koans.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! 

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Thank you image
Next article
Literals and Keyword Literals in ruby
Live masterclass