Ruby Operators
Introduction
Operators in Ruby are special symbols or keywords used to perform operations on data. Ruby supports various types of operators that are used to perform arithmetic, logical, bitwise, and comparison operations.
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on numbers.
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
** | Exponentiation |
Example
a = 5
b = 10
puts a + b
puts a - b
puts a * b
puts a / b
puts a % b
puts a ** b
Output
15
-5
50
0
5
9765625
Comparison Operators
Comparison operators are used to compare two values and return a boolean value.
Operator | Description |
---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Example
a = 5
b = 10
puts a == b
puts a != b
puts a > b
puts a < b
puts a >= b
puts a <= b
Output
false
true
false
true
false
true
Logical Operators
Logical operators are used to perform logical operations on expressions that return boolean values.
Operator | Description |
---|---|
and | Returns true if both expressions are true |
or | Returns true if either expression is true |
not | Returns true if the expression is false |
Example
a = true
b = false
puts a and b
puts a or b
puts not a
Output
false
true
false
Bitwise Operators
Bitwise operators are used to perform bitwise operations on integers.
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
~ | Bitwise NOT |
^ | Bitwise XOR |
<< | Left shift |
>> | Right shift |
Example
a = 0b1010
b = 0b1100
puts a & b
puts a | b
puts ~a
puts a ^ b
puts a << 2
puts a >> 2
Output
8
14
-11
6
40
2
Assignment Operators
Assignment operators are used to assign a value to a variable.
Operator | Description |
---|---|
= | Assigns a value to a variable |
+= | Adds a value to a variable and assigns the result |
-= | Subtracts a value from a variable and assigns the result |
*= | Multiplies a variable by a value and assigns the result |
/= | Divides a variable by a value and assigns the result |
%= | Computes the modulus of a variable and assigns the result |
**= | Raises a variable to a power and assigns the result |
&= | Performs a bitwise AND on a variable and assigns the result |
|= | Performs a bitwise OR on a variable and assigns the result |
^= | Performs a bitwise XOR on a variable and assigns the result |
<<= | Shifts the bits of a variable to the left and assigns the result |
>>= | Shifts the bits of a variable to the right and assigns the result |
Example
a = 5
a += 5
puts a
a -= 3
puts a
a *= 2
puts a
a /= 3
puts a
a %= 4
puts a
a **= 3
puts a
a &= 0b1010
puts a
a |= 0b0101
puts a
a ^= 0b0011
puts a
a <<= 2
puts a
a >>= 1
puts a
Output
10
7
14
4
0
0
0
5
6
24
12
Summary
Operators in Ruby are special symbols or keywords used to perform operations on data. Ruby supports various types of operators that are used to perform arithmetic, logical, bitwise, and comparison operations. In this article, we covered the most used operators in Ruby with examples and output.