Ruby Tutorial Ruby References

Ruby - Operators



Operators are used to perform operation on a single operand or two operands. Operators in Ruby can be categorized as follows:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators
  • Miscellaneous operators

Ruby Arithmetic operators

Arithmetic operators are used to perform arithmetic operations on two operands.

OperatorNameDescription
+AdditionAdd two values
-SubtractionSubtract one value from another
*MultiplicationMultiply two values
/DivisionDivide one value by another
%ModuloReturns remainder of division operation
**Exponent / PowerReturns first operand raised to the power of second operand

Example

Ruby Assignment operators

Assignment operators are used to assign values of right hand side expression to left hand side operand.

OperatorExpressionEquivalent toDescription
=a = 5a = 5Example
+=a += ba = a + b
-=a -= ba = a - b
*=a *= ba = a * b
/=a /= ba = a / b
%=a %= ba = a % b
**=a **= ba = a ** b
&=a &= ba = a & bMore Info
|=a |= b a = a | bMore Info
^=a ^= b a = a ^ bMore Info
>>=a >>= ba = a >> bMore Info
<<=a <<= ba = a << bMore Info

Ruby Comparison operators

Comparison operators are used to compare values of two operands. It returns true when values matches and returns false when values does not match.

Operator Description
==Equal: Checks the values of two operands and returns true if they are same.
!=Not equal: Checks the values of two operands and returns true if they are not same.
===Case equality: A === B, Checks if B is a member of the set of A.
>Greater than: Checks the values of two operands and returns true if the value of first operand is greater than the value of second operand.
<Less than: Checks the values of two operands and returns true if the value of first operand is less than the value of second operand.
>=Greater than or equal to: Checks the values of two operands and returns true if the value of first operand is greater than or equal to the value of second operand.
<=Less than or equal to: Checks the values of two operands and returns true if the value of first operand is less than or equal to the value of second operand.
<=>Combined Comparison Operator: Checks the values of two operands and returns values based on the values of two operands:
  • Returns -1 if the value of first operand is less than value of second operand.
  • Returns 0 if the values of two operands are equal.
  • Returns 1 if the value of first operand is greater than value of second operand.
.eql?Equal: Checks the values and datatypes of two operands and returns true if they are same.

Example

Ruby Logical operators

Logical operators are used to combine two or more conditions.

Operator NameDescription
and Logical ANDReturns true when all conditions are true
&&
orLogical ORReturns true when any of the conditions is true
||
notLogical NOTReturns true when given conditions is not true
!

More Info

Ruby Bitwise operators

Bitwise operators are used to perform bitwise operations on two operands.

OperatorNameDescriptionMore Info
&ANDReturns 1 if both bits at the same position in both operands are 1, else returns 0.More Info
|ORReturns 1 if one of two bits at the same position in both operands is 1, else returns 0.More Info
^XORReturns 1 if only one of two bits at the same position in both operands is 1, else returns 0.More Info
~NOTReverse all the bits.More Info
>>Right shiftThe left operand is moved right by the number of bits present in the right operand.More Info
<<Left shiftThe left operand value is moved left by the number of bits present in the right operand.More Info

Ruby Miscellaneous operators

The table below describes other operators supported by Ruby:

OperatorDescription
ternary operator (?:)Returns one of the two values based on value of boolean expression.
range operator (..)Creates a range from start point to end point inclusive.
range operator (...)Creates a range from start point to end point exclusive.

Ruby Operators Precedence

Operator precedence (order of operations) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.

For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 52 = 28 and 3 × 52 = 75.

The following table lists the precedence and associativity of Ruby operators. Operators are listed top to bottom, in descending precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence.

PrecedenceOperatorDescription
21!Logical NOT
~Bitwise NOT
+aUnary plus
20**Exponential operator
19-aUnary minus
18*  /  %Multiplication, Division, Remainder
17+  -Addition, Subtraction
16<<  >>Bitwise left shift and right shift
15&Bitwise AND
14|Bitwise OR
^Bitwise XOR
13<  <=  >  >=Less than, Less than or equal, Greater than, and Greater than or equal
12==  !=Equality and Inequality
===  <=>Case equality, Combined Comparison Operator
=~  !~Pattern matching operators
11&&Logical AND
10||Logical OR
9..Range creation operators
...
8a?b:cternary (conditional) operator
7modifier-rescueException-handling modifier
6=Direct assignment
+=  -=  *=  /=  %=  **=Compound assignment by sum, difference, product, quotient, remainder and exponential
<<=  >>=Compound assignment by Bitwise left shift and right shift
&=  ^=  |=Compound assignment by Bitwise AND, XOR and OR
5defined?Test variable definition and type
4notLogical NOT
3andLogical AND
orLogical OR
2modifier-if  modifier-unless  modifier-while  modifier-untilConditional and loop modifiers
1{ }blocks