Ruby | Operators (original) (raw)

Last Updated : 23 Apr, 2024

An operator is a symbol that represents an operation to be performed with one or more operand. Operators are the foundation of any programming language. Operators allow us to perform different kinds of operations on operands. There are different types of operators used in Ruby as follows:

Arithmetic Operators

These are used to perform arithmetic/mathematical operations on operands.

**Example:

Ruby `

Ruby program to demonstrate

the Arithmetic Operators

Addition

puts ("Addition:") puts (10 + 20)

Subtraction

puts ("Subtraction:") puts (40 - 20)

Division

puts ("Division:") puts (100 / 20)

Multiplication

puts ("Multiplication:") puts (10 * 20)

Modulus

puts ("Modulus:") puts (20 % 7)

Exponent

puts ("Exponent:") puts (2 ** 4)

`

**Output:

Addition: 30 Subtraction: 20 Division: 5 Multiplication: 200 Modulus: 6 Exponent: 16

Comparison Operators

Comparison operators or Relational operators are used for comparison of two values. Let’s see them one by one:

**Example:

Ruby `

Ruby program to demonstrate

the Comparison Operators

puts "Equal To Operator:" puts (10 == 20)

puts "Not Equal To Operator:" puts (40 != 20)

puts "Greater than Operator" puts (100 > 20)

puts "Less than Operator" puts (10 < 20)

puts "Less than Equal To Operator" puts (2 <= 5)

puts "Greater than Equal To Operator" puts (2 >= 5)

puts "Combined combination operator" puts(20 <=> 20) puts(10 <=> 20) puts(20 <=> 10)

`

**Output:

Equal To Operator: false Not Equal To Operator: true Greater than Operator true Less than Operator true Less than Equal To Operator true Greater than Equal To Operator false Combined combination operator 0 -1 1

Logical Operators

They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. They are described below:

**Example:

Ruby `

Ruby program to demonstrate

the Logical Operators

Variables

a = 10 b = 20 c = 30

using && operator

if a == 10 && b == 20 && c == 30 puts "Logical AND Operator" puts result = a * b * c end

using || operator

puts "Logical OR operator" if a == 10 || b == 20 puts result = a + b + c end

using ! operator

puts "Logical Not Operator" puts !(true)

`

**Output:

Logical AND Operator 6000 Logical OR operator 60 Logical Not Operator false

Assignment Operators

Assignment operators are used to assigning a value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error.
Different types of assignment operators are shown below:

**Example:

Ruby `

Ruby program to demonstrate

the Assignments Operators

puts "Simple assignment operator" puts a = 20

puts "Add AND assignment operator" puts a += 10

puts "Subtract AND assignment operator" puts a -= 5

puts "Multiply AND assignment operator" puts a *= 10

puts "Divide AND assignment operator" puts a /= 4

puts "Modulus AND assignment operator" puts a %= 3

puts "Exponent AND assignment operator" puts a **= 3

`

**Output:

Simple assignment operator 20 Add AND assignment operator 30 Subtract AND assignment operator 25 Multiply AND assignment operator 250 Divide AND assignment operator 62 Modulus AND assignment operator 2 Exponent AND assignment operator 8

Bitwise Operators

In Ruby, there are 6 bitwise operators which work at bit level or used to perform bit by bit operations. Following are the bitwise operators :

**Example:

Ruby `

Ruby program to demonstrate

the Bitwise Operators

variables

a = 10 b = 20

puts "Bitwise AND operator" puts (a & b)

puts "Bitwise OR operator" puts (a |b)

puts "Bitwise XOR operator" puts (a ^ b)

puts "Bitwise Complement operator" puts (~a)

puts "Binary right shift operator" puts (a >> 2)

puts "Binary left shift operator" puts (a << 2)

`

**Output:

Bitwise AND operator 0 Bitwise OR operator 30 Bitwise XOR operator 30 Bitwise Complement operator -11 Binary right shift operator 2 Binary left shift operator 40

Ternary Operator

It is a conditional operator which is a shorthand version of the if-else statement. It has three operands and hence the name ternary. It will return one of two values depending on the value of a Boolean expression.
**Syntax :

condition ? first_expression : second_expression;

**Explanation :

condition: It be evaluated to true or false. If the condition is true first_expression is evaluated and becomes the result. If the condition is false, second_expression is evaluated and becomes the result.

**Example :

Ruby `

Ruby program to demonstrate

the Ternary Operator

variable

marks_obtained = 100

using ternary operator

result = marks_obtained > 40 ? 'Pass' : 'Fail'

displaying output

puts result

`

**Output:

Pass

Range Operators

In Ruby, range operators are used for creating the specified sequence range of specified elements. There are two range operators in Ruby as follows:

**Example:

Ruby `

Ruby program to demonstrate

the Range Operator

Array value separator

$, =", "

using .. Operator

range_op = (7 .. 10).to_a

displaying result

puts "#{range_op}"

using ... Operator

range_op1 = (7 ... 10).to_a

displaying result

puts "#{range_op1}"

`

**Output:

[7, 8, 9, 10] [7, 8, 9]

defined? Operator

The defined? the operator is a special operator which is used to check whether the passed expression is defined or not. It returns _nil if passed argument is not defined, otherwise, it returns a string of that argument which defines that.
**Syntax:

defined? expression_to_be_checked

**Example:

Ruby `

Ruby program to demonstrate

the defined? Operator

variables

GFG = 1 Geeks = 70

puts ("define? Operator Results")

using defined? Operator

it returns constant

puts defined? GFG

it returns constant

puts defined? Geeks

it returns nil

puts defined? a

it returns expression

puts defined? 50

`

**Output:

define? Operator Results constant constant

expression

Dot "." and Double Colon "::" Operators

**Example:

Ruby `

Ruby program to demonstrate

Dot “.” and Double Colon

“::” Operators

defined constant on main Object class

CONS = 5

define module

module Geeks

CONS = 5

set global CONS to 7

::CONS = 7

# set local CONS to 10

CONS = 10
end

displaying global CONS value

puts CONS

displaying local "Geeks" CONS value

using :: operator

puts Geeks::CONS

class Gfg def Geeks2 puts "Dot Operator" end end

calling Geeks2 module using

Dot(.) operator

puts Gfg.new.Geeks2

`

**Output:

7 10 Dot Operator main.rb:14: warning: already initialized constant CONS main.rb:6: warning: previous definition of CONS was here main.rb:17: warning: already initialized constant Geeks::CONS main.rb:11: warning: previous definition of CONS was here