Operators in Ruby

There are a number of operators in ruby. Operators are those basic elements which do some operations on data like addition, subtraction, etc.

Arithmetic Operators

These are mathematics operators.

+addition4 + 5 = 9
-subtraction12 - 34 = -22
*Multiplication2*3 = 6
/Division, if both number are integers then integer is returned3/2=1 but 3.0/2 = 1.5
%modulus, return reminder after division5/2 = 1, 15/4 = 3
**power2**3 = 8 (2*2*2 = 8)

Comparison Operators

These operators compare condition and return true or false.

==return true if both are equal otherwise false2==2 is true
!=return true if both sides are not equal2!=2 is false
>return true if left side is greater4>2 is true
<return true if right side is greater2<4 is true
>=return true if right side is grater or equal4>=4,5>=4, both true
<=return true if left side is grater or equal4<=4,3<=4, both true
<=>return 0 if both equal, 1 if left greater and -1 if right greater4<=>3 returns 1
===return true if right is in the range of left(1...20) === 9 returns true.

Assignment Operators:

These operators assign the value to any variable after doing operation.

=assign valuea = 12
+=add right side to left sidea+=b same as a = a+b
-=subtract right side from left sidea-=b same as a = a-b
*=multiply right side to left sidea*=b same as a = a*b
/=divide right side by left sidea/=b same as a = a/b
%=divide right side by left side and assign reminder to right sidea%=b same as a = a%b
**=raise left side index as right sidea**=b same as a = a**b

Parallel Assignment:

In ruby, we can assign values to multiple variables in same line.

  1. # variables as well as values are saperated by comma.
  2. name, age = 'harish',20
  3. print "name is #{name} and age is #{age}"


  1. name is harish and age is 20

Bitwise Operators:

If you are aware of binary numbers then ruby also provides bitwise operators. Lets take an example a = 23 (in binary, a =10111) and b = 43 (in binary, b = 101011). a&b; =3 (in binary, 000011). '&' is Binary AND operator. All operators are similar.

&Binary AND operatora&b; = 3
|Binary OR operatora|b = 63
^Binary XOR operatora^b = 60
~Binary AND operator~23 = -24
>>Right shift operator23>>2 = 95
<<Left shift operator23<<2 = 5

Logical Operators:

These operators are also compare values and return true or false.

andboth sides are true
orat least one side is true
&&both sides are true
||at least one side is true
!reverse the logical state, true becomes false
notsame as !

About Harish Kumar

Harish, a technical core team member at www.lyflink.com with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and business topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Complete Python Bootcamp: Go from zero to hero in Python 3

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites

Meet on Twitter


Subscribe now and get weekly updates directly in your inbox!

Any Course on Udemy in $6 (INR 455)

Development Category (English)300x250

Best Udemy Courses

PHP with Laravel for beginners - Become a Master in Laravel