Updated March 28, 2023
What are Ruby Operators?
Ruby supports a wide collection of operators. In addition, the majority of operators are system calls. For example, a+ b is interpreted as a.+(b), where the argument for the+ method in the object referred to by variable a is called with b.
Operators like + – * / % ** & | ^ << >> && ||, there is a corresponding form of abbreviated assignment operator (+= -= etc.).
Top 8 Ruby Operators
Below is the list of the ruby operator with their short description and examples.
1. Arithmetic Operators
Ruby Arithmetic operators are used to perform arithmetic operations. Below are the arithmetic operators used in ruby
- + : This arithmetic operator is used to add values of two operands.
- – : This subtraction operator is used to subtract the value of the second operand from the value of the first operand.
- * : This multiplication operator is used to multiply the values of two operands.
- / : This division operator is used to divide numerator by denominator.
- % : This modulus operator is used to find the modulus.
- ** : This operator is used to make the value right-side operand exponent to a value of left side operand.
Example
Code:
a = 6
b = 3
puts("Addition:")
puts(a + b)
puts("Subtraction:")
puts(a - b)
puts("Multiplication:")
puts(a * b)
puts("Division:")
puts(a / b)
puts("Exponential")
puts(a ** b)
puts("Modulus")
puts(a % b)
Output:
2. Comparison Operators
Comparison operators compare the values and print the result. Below are the comparison operators bused in ruby.
- ==: This equals the operator will assign the value of the right side operand to the left side operand.
- >: This greater than operator is used to check the greater value.
- <: This less-than operator is used to check the small value.
- >=: This greater than equal to the operator is used to check greater or equal value.
- <=: This less than equal to the operator is used to check the small value or equal value.
- !=: This not equal to operator chek the value and print true if condition matches otherwise will print false.
- <=>: This combined comparison operator will compare the values.
- 8.eql?: This operator is used to check equality and type of operands.
Example
Code:
a = 6
b = 3
puts(a == b)
puts(a != b)
puts(a > b)
puts(a < b)
puts(a >= b)
puts(a <= b)
puts(a <=> b)
puts(a .eql? b)
Output:
3. Logical Operators
Logical operators work on bit values of the operand. Below are the logical operators used in Ruby
- &&: This AND operator will perform the Anding operation on two operands. This operator will print true if all values are true and false if all values are false or one of the values is false.
- ||: This OR operators will perform the or operation on two operands. This operator will print true if all values are true or one of the values is true and will print false if all values are false.
Example
Code:
a = 6
b = 3
puts(a && b)
puts(a || b)
Output:
4. Assignment Operators
Assignment operators are used to assigning the values. Below is the assignment operators bused in ruby
- =: This operator will assign the value of the right operand to left.
- +=: This add assignment operator will add values and assign them to the left operand.
- -=: This Subtract assignment operator will subtract values and assign them to the left operand.
- *=: This multiplication assignment operator will multiply values and assign them to the left operand.
- /=: This divide assignment operator will perform divide operation
- %=: This modulus assignment operator will perform modulus operation
- **=: This exponential assignment operator will perform the exponential operation.
Example
Code:
a = 6
b = 3
puts(a = b)
puts(a += b)
puts(a *= b)
puts(a /= b)
puts(a **= b)
puts(a %= b)
Output:
5. Bitwise Operators
Bitwise operators work on bits. Below are the bitwise operators used in ruby.
- 1.&: This AND operator will perform AND operation on bits.
- 2.|: This OR operator will perform OR operation.
- <<: This operator will shift the bits left side.
- >>: This operator will shift the bits right side.
- ^: This operator will perform the XOR operation.
- ~: This complement operator will return the opposite value.
Example
Code:
a = 6
b = 3
puts(a & b)
puts(a | b)
puts(a << b)
puts(a >> b)
puts(a ^ b)
puts(~a)
Output:
6. Unary Operator
Unary operators works on a single operand. Below are the unary operators used in Ruby.
- !: This operator is used to perform the bitwise NOT operation on bits.
- ~: This operator is used to perform the bitwise compliment on bits.
- +: This operator is used to perform the addition.
Example
Code:
a = 6
puts(+a)
puts(!a)
puts(~a)
Output:
7. Ternary Operators
Ternary operators are used to evaluate the condition and return the result based on the condition. in ruby ternary operator is denoted by ?:
Example
Code:
a = 6
b = 3
puts(b < a ? a : b)
puts(a < b ? a : b)
Output:
8. Range Operators
Range operators are used to check the values between the range only. Below are the range operators used in ruby
- ..: This operator is used to create the range including the last term.
- …: This operator is used to create the range excluding the last term.
Example
Code:
a = 6
Result = case a
when 0..5 then "Fail"
when 6..10 then "Pass"
end
puts Result
Output:
Recommended Articles
We hope that this EDUCBA information on “Ruby Operators” was beneficial to you. You can view EDUCBA’s recommended articles for more information.