Ruby Operators

Ruby operators: Here, we are going to learn about the various types of the operators of Ruby programming language with examples.
Submitted by Hrithik Chandra Prasad, on July 26, 2019

Ruby operators

Operators are the symbols which assist compiler or interpreter to carry out certain mathematical, logical and relational tasks and produce the results. Operators are method calls with parameters.

Types of Ruby operators

Ruby supports a variety of operators but following are few of them:

1. Arithmetic operators
2. Comparison operators
3. Assignment operators
4. Bitwise operators

1) Arithmetic Operators

They are mathematical methods that takes two parameters and perform arithmetic operations on them. Following is the list of arithmetic operators,

Symbol Name Description
+ Addition It Adds two provided values
- Subtraction It Subtracts two given values
/ Division Used to Divide left hand operand by right hand operand
* Multiplication Used to multiply operands present on both the sides.
% Modulus Gives remainder as result by dividing left hand side operand with the right hand side operand.
** Exponent It is used to carry out power calculations on operands.

Example:

puts "Enter first value"
num1 = gets.chomp.to_i
puts "Enter second value"
num2 = gets.chomp.to_i

puts "Sum is #{add}"

sub=num1-num2
puts "Difference is #{sub}"

mul=num1*num2
puts "Product  is #{mul}"

div=num1/num2
puts "Division is #{div}"

mod=num1%num2
puts "Remainder is #{mod}"

Output

Enter first value
10
Enter second value
3
Sum is 13
Difference is 7
Product  is 30
Division is 3
Remainder is 1

2) Comparison Operator

They are a variant of binary operators which takes two operands and carry out comparison between them.

Following is the list of comparison operators,

Symbol Description
== It is used to check whether the two specified operands are equal or not. If yes, then condition becomes true.
!= When there is a need to check whether the values of operands are not equal to each other, then this operator is used.
> Used to check if the left hand side value is greater than right hand side operand, if yes then the condition becomes true.
< Used to check if the right hand side value is greater than left hand side operand, if yes then the condition becomes true.
>= Used to check if the left hand side value is greater than or equal to right hand side operand, if yes then the condition becomes true.
<= Used to check if the right hand side value is greater than or equal to left hand side operand, if yes then the condition becomes true.
<=> It returns 0 if first value is equal second value, 1 if first operand is greater than the second operand and -1 if first operand is less than the later one.
.eql? It returns true if the receiver and the arguments are of same data type and have equal values.
.equal? It returns true if the receiver and the arguments hail from same object id.

Example:

puts "Enter first value"
num1 = gets.chomp.to_i
puts "Enter second value"
num2 = gets.chomp.to_i

if num1==num2
puts "Both are equal"
elsif num1>num2
puts "num1 is greater than num2"
elsif num1<num2
puts "num2 is greater than num1"
elsif num1!=num2
puts "num2 is not equal to num1"
elsif num1>=num2
puts "num1 is greater than or equals to num2"
elsif num1<=num2
puts "num2 is greater than or equals to num1"
end

Output

Enter first value
100
Enter second value
231
num2 is greater than num1

3) Assignment operators

Assignment operators are used to assign the value of right hand side operator to the left hand side operator.

Following is the list of assignment operators which are supported in Ruby,

Operator Description
= Used to assign right hand value to the left hand operand.
+= Adds left hand operand with the right hand operand and stores the result in left hand operand.
-= Subtracts right hand operand from the left hand operand and stores the result in left hand operand.
*= Multiplies left hand operand with the right hand operand and stores the product in left hand operand.
%= Used to find modulus by dividing and stores remainder in left hand operand.
/= Used to find division and stores result in left hand operand
**= Used to find exponential power and keeps the result in the left operand after computation.

Example:

puts "Enter first value"
num1 = gets.chomp.to_i
puts "Enter second value"
num2 = gets.chomp.to_i

puts "Enter choice:"
ch = gets.chomp

case ch
when 'a'
num1+=1
puts "#{num1}"
when 'b'
num2-=2
puts "#{num2}"
when 'c'
num1*=2
puts "#{num1}"
when 'd'
num1**=9
puts "#{num1}"
else
puts "option not available."
end

Output

Enter first value
10
Enter second value
2
Enter choice:
c
20

4) Bitwise operators

They are used to perform bitwise operations on binary number which includes change of particular bit.

Following is the list of bitwise operators which are supported by Ruby,

Symbol Name Description
& Binary AND It works by copying a bit to the result if it is present in both the operands
| Binary OR It works by copying a bit to the result if it is present in one of the operands.
^ Binary XOR It works by copying a bit to the result if it is present in one of the operand but not in both.
~ Binary 1's complement It flips the bits like making 1 to 0 and vice versa.
>> Binary left shift It shifts the bit to the left by number of bits told by the left operand.
<< Binary Right shift It shifts the bit to the right by number of bits told by the right operand.

Example:

puts "Enter first value"
num1 = gets.chomp.to_i
puts "Enter second value"
num2 = gets.chomp.to_i

a1=num1 & num2
puts "and is #{a1}"

a2=num1 | num2
puts "OR is #{a2}"

a3=num1^num2
puts "XOR is #{a3}"

a4= ~num2
puts "Complement is #{a4}"

a5=num1>>num2
puts "left shift is #{a5}"

a6=num1<<num2
puts "Right shift is #{a6}"

Output

Enter first value
3
Enter second value
5
and is 1
OR is 7
XOR is 6
Complement is -6
left shift is 0
Right shift is 96

TOP Interview Coding Problems/Challenges

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.