Ruby Operators

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Ruby Operators

Ruby has a built-in modern set of operators. Operators are a symbol which is used to perform different operations. For example, +, -, /, *, etc.


Types of operators:

  • Unary operator
  • Airthmetic operator
  • Bitwise operator
  • Logical operator
  • Ternary operator
  • Assignment operator
  • Comparison operator
  • Range operator

Unary Operator

Unary operators expect a single operand to run on.

Operator Description
! Boolean NOT
~ Bitwise complement
+ Unary plus

Example

In file hello.rb, write the following code.

  1. #!/usr/bin/ruby -w   
  2.    
  3.  puts("Unary operator")   
  4.  puts(~5)   
  5.  puts(~-5)   
  6.  puts(!true)   
  7.  puts(!false

Output:

Ruby operators 1

Airthmetic Operator

Airthmetic operators take numerical values as operands and return them in a single value.

Operator Description
+ Adds values from both sides of the operator.
- Subtract values from both sides of the operator.
/ Divide left side operand with right side operand.
* Multiply values from both sides of the operator.
** Right side operand becomes the exponent of left side operand.
% Divide left side operand with right side operand returning remainder.

Example

In file hello.rb, write the following code.

  1. #!/usr/bin/ruby -w   
  2.   
  3.  puts("add operator")   
  4.  puts(10 + 20)      
  5.  puts("subtract operator")   
  6.  puts(35 - 15)    
  7.  puts("multiply operator")   
  8.  puts(4 * 8)   
  9.  puts("division operator")   
  10.  puts(25 / 5)   
  11.  puts("exponential operator")   
  12.  puts(5 ** 2)   
  13.  puts("modulo operator")   
  14.  puts(25 % 4)   

Output:

Ruby operators 2

Bitwise Operator

Bitwise operators work on bits operands.

Operator Description
& AND operator
| OR operator
<< Left shift operator
>>  Right shift operator
^ XOR operator
~ Complement operator

Logical Operator

Logical operators work on bits operands.

Operator Description
&& AND operator
|| OR operator

Ternary Operator

Ternary operators first check whether given conditions are true or false, then execute the condition.

Operator Description
?: Conditional expression

Example

In file hello.rb, write the following code.

Output:

Ruby operators 3

Assignment Operator

Assignment operator assign a value to the operands.

Operator Description
= Simple assignment operator
+= Add assignment operator
-= subtract assignment operator
*= Multiply assignment operator
/= Divide assignment operator
%= Modulus assignment operator
**= Exponential assignment operator

Comparison Operator

Comparison operators compare two operands.

Operator Description
== Equal operator
!= Not equal operator
> left operand is greater than right operand
Right operand is greater than left operand
>= Left operand is greater than or equal to right operand
<= Right operand is greater than or equal to left operand
<=> Combined comparison operator
.eql? Checks for equality and type of the operands
equal? Checks for the object ID

Example

In file hello.rb, write the following code.

  1. #!/usr/bin/ruby -w   
  2.    
  3.  puts("Comparison operator")   
  4.  puts(2 == 5)   
  5.  puts(2 != 5)   
  6.  puts(2 > 5)   
  7.  puts(2 < 5)   
  8.  puts(2 >= 5)   
  9.  puts(2 <= 5)   

  Output:

Ruby operators 4

Range Operator

Range operators create a range of successive values consisting of a start, end and range of values in between.

The (..) creates a range including the last term and (...) creates a range excluding the last term.

For example, for the range of 1..5, output will range from 1 to 5.

and for the range of 1...5, output will range from 1 to 4.

Operator Description
.. Range is inclusive of the last term
... Range is exclusive of the last term