SlideShare a Scribd company logo
RUBY
OPERATORS
Hoang Quan(BlazingRockStorm)
Contents
Ruby Arithmetic Operators
1.
Ruby Comparison Operators
2.
Ruby Assignment Operators
3.
Ruby Parallel Assignment
4.
Ruby Bitwise Operators
5.
Ruby Logical Operators
6.
Ruby Ternary Operator
7.
Ruby Range Operators
8.
Ruby defined? Operators
9.
Ruby Dot "." and Double Colon
"::" Operators
10.
Ruby Operators Precedence
11.
Arithmetic Operators
Sr.No. Operator & Description Example
1 + Addition − Adds values on either side of the operator. a + b will give ?
2
− Subtraction − Subtracts right hand operand from left hand
operand.
a - b will give ?
3 * Multiplication − Multiplies values on either side of the operator. a * b will give ?
4 / Division − Divides left hand operand by right hand operand. b / a will give ?
5
% Modulus − Divides left hand operand by right hand operand and
returns remainder.
b % a will give ?
6
** Exponent − Performs exponential (power) calculation on
operators.
a**b will give 10 to the power ?
Comparison Operators
Sr.No. Operator & Description Example
1
==
Checks if the value of two operands are equal or not, if yes then
condition becomes true.
(a == b) is not true.
2
!=
Checks if the value of two operands are equal or not, if values are
not equal then condition becomes true.
(a != b) is true.
3
>
Checks if the value of left operand is greater than the value of
right operand, if yes then condition becomes true.
(a > b) is not true.
4
<
Checks if the value of left operand is less than the value of right
operand, if yes then condition becomes true.
(a < b) is true.
5
>=
Checks if the value of left operand is greater than or equal to the
value of right operand, if yes then condition becomes true.
(a >= b) is not true.
Sr.No. Operator & Description Example
6
<=
Checks if the value of left operand is less than or equal to the
value of right operand, if yes then condition becomes true.
(a <= b) is true.
7
⇔
Combined comparison operator. Returns 0 if first operand equals
second, 1 if first operand is greater than the second and -1 if first
operand is less than the second.
(a <=> b) returns -1.
8
===
Used to test equality within a when clause of a casestatement.
(1...10) === 5 returns true.
9
.eql?
True if the receiver and argument have both the same type and
equal values.
1 == 1.0 returns true, but 1.eql?(1.0) is false.
10
equal?
True if the receiver and argument have the same object id.
if aObj is duplicate of bObj
then aObj == bObj is true,
a.equal?bObj is false
but a.equal?aObj is true
Assignment Operators
Sr.No. Operator & Description Example
1
=
Simple assignment operator, assigns values from right side
operands to left side operand.
c = a + b will assign the value of a + b into c
2
+=
Add AND assignment operator, adds right operand to the left
operand and assign the result to left operand.
c += a is equivalent to c = c + a
3
-=
Subtract AND assignment operator, subtracts right operand from
the left operand and assign the result to left operand.
c -= a is equivalent to c = c - a
4
*=
Multiply AND assignment operator, multiplies right operand with
the left operand and assign the result to left operand.
c *= a is equivalent to c = c * a
Sr.No. Operator & Description Example
5
/=
Divide AND assignment operator, divides
left operand with the right operand and
assign the result to left operand.
c /= a is equivalent to c = c / a
6
%=
Modulus AND assignment operator, takes
modulus using two operands and assign
the result to left operand.
c %= a is equivalent to c = c % a
7
**=
Exponent AND assignment operator,
performs exponential (power) calculation
on operators and assign value to the left
operand.
c **= a is equivalent to c = c ** a
Parallel Assignment
Parallel Assignment
Ruby also supports the parallel assignment of variables. This enables
multiple variables to be initialized with a single line of Ruby code
Parallel Assignment
Parallel assignment is also useful for swapping the values held in two
variables −
Bitwise Operators
Bitwise Operators
Bitwise operator works on bits and performs bit by bit operation.
Assume if a = 60; and b = 13; now in binary format they will be as
follows
Sr.No. Operator & Description Example
1
&
Binary AND Operator copies a bit to the result if it exists in both
operands.
(a & b) will give 12, which is 0000 1100
2
|
Binary OR Operator copies a bit if it exists in either operand.
(a | b) will give 61, which is 0011 1101
3
^
Binary XOR Operator copies the bit if it is set in one operand but
not both.
(a ^ b) will give 49, which is 0011 0001
Sr.No. Operator & Description Example
4
~
Binary Ones Complement Operator is unary and has the effect of
'flipping' bits.
(~a ) will give -61, which is 1100 0011 in 2's complement form due to
a signed binary number.
5
<<
Binary Left Shift Operator. The left operands value is moved left by
the number of bits specified by the right operand.
a << 2 will give 240, which is 1111 0000
6
>>
Binary Right Shift Operator. The left operands value is moved right
by the number of bits specified by the right operand.
a >> 2 will give 15, which is 0000 1111
Logical Operators
Sr.No. Operator & Description Example
1
and
Called Logical AND operator. If both the operands are true, then
the condition becomes true.
(a and b) is true.
2
or
Called Logical OR Operator. If any of the two operands are non
zero, then the condition becomes true.
(a or b) is true.
3
&&
Called Logical AND operator. If both the operands are non zero,
then the condition becomes true.
(a && b) is true.
Sr.No. Operator & Description Example
4
||
Called Logical OR Operator. If any of the two operands are non
zero, then the condition becomes true.
(a || b) is true.
5
!
Called Logical NOT Operator. Use to reverses the logical state of
its operand. If a condition is true, then Logical NOT operator will
make false.
!(a && b) is false.
6
not
Called Logical NOT Operator. Use to reverses the logical state of
its operand. If a condition is true, then Logical NOT operator will
make false.
not(a && b) is false.
Ternary Operator
Sr.No. Operator & Description Example
1
? :
Conditional Expression
If Condition is true ? Then value X :
Otherwise value Y
Range Operators
Sr.No. Operator & Description Example
1
..
Creates a range from start point to
end point inclusive.
1..10 Creates a range from 1 to 10
inclusive.
2
...
Creates a range from start point to
end point exclusive.
1...10 Creates a range from 1 to 9.
Range Operators
defined? Operators
defined? Operators
defined? is a special operator that takes the form of a method call to
determine whether or not the passed expression is defined. It returns
a description string of the expression, or nil if the expression isn't
defined.
Dot "." and Double Colon
"::" Operators
04. Ruby Operators Slides - Ruby Core Teaching
Operators Precedence
Method Operator Description
&& Logical 'AND'
|| Logical 'OR'
.. ... Range (inclusive and exclusive)
? : Ternary if-then-else
= %= { /= -= += |= &= >>= <<= *= &&= ||= **= Assignment
defined? Check if specified symbol defined
not Logical negation
or and Logical composition
Thank you!
ありがとうございます!

More Related Content

Similar to 04. Ruby Operators Slides - Ruby Core Teaching (20)

itft-Operators in java
itft-Operators in javaitft-Operators in java
itft-Operators in java
Atul Sehdev
 
Operators in C & C++ Language
Operators in C & C++ LanguageOperators in C & C++ Language
Operators in C & C++ Language
PreSolutions Softwares
 
Constructor and destructors
Constructor and destructorsConstructor and destructors
Constructor and destructors
divyalakshmi77
 
C# operators
C# operatorsC# operators
C# operators
baabtra.com - No. 1 supplier of quality freshers
 
Python notes for students to develop and learn
Python notes for students to develop and learnPython notes for students to develop and learn
Python notes for students to develop and learn
kavithaadhilakshmi
 
Operators
OperatorsOperators
Operators
ShiyamalaSrinivasan
 
Python operators
Python operatorsPython operators
Python operators
SaurabhUpadhyay73
 
Java 2
Java 2Java 2
Java 2
Preethi Nambiar
 
Java - Operators
Java - OperatorsJava - Operators
Java - Operators
Preethi Nambiar
 
C Operators
C OperatorsC Operators
C Operators
Yash Modi
 
Operators
OperatorsOperators
Operators
jayesh30sikchi
 
Lecture-06 Operators.pdf
Lecture-06 Operators.pdfLecture-06 Operators.pdf
Lecture-06 Operators.pdf
NikhilSoni177492
 
Operators and it's type
Operators and it's type Operators and it's type
Operators and it's type
Asheesh kushwaha
 
Operators Concept in Python-N.Kavitha.pptx
Operators Concept in Python-N.Kavitha.pptxOperators Concept in Python-N.Kavitha.pptx
Operators Concept in Python-N.Kavitha.pptx
Kavitha713564
 
Operators1.pptx
Operators1.pptxOperators1.pptx
Operators1.pptx
HARSHSHARMA840
 
Opeartor &amp; expression
Opeartor &amp; expressionOpeartor &amp; expression
Opeartor &amp; expression
V.V.Vanniapermal College for Women
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
Manoj Tyagi
 
Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5
Mohd Harris Ahmad Jaal
 
Operator.ppt
Operator.pptOperator.ppt
Operator.ppt
Darshan Patel
 
Operators
OperatorsOperators
Operators
Devi Pradeep Podugu
 

More from quanhoangd129 (8)

01. Ruby Introduction - Ruby Core Teaching
01. Ruby Introduction - Ruby Core Teaching01. Ruby Introduction - Ruby Core Teaching
01. Ruby Introduction - Ruby Core Teaching
quanhoangd129
 
09. Ruby Object Oriented Programming - Ruby Core Teaching
09. Ruby Object Oriented Programming - Ruby Core Teaching09. Ruby Object Oriented Programming - Ruby Core Teaching
09. Ruby Object Oriented Programming - Ruby Core Teaching
quanhoangd129
 
07. Ruby String Slides - Ruby Core Teaching
07. Ruby String Slides - Ruby Core Teaching07. Ruby String Slides - Ruby Core Teaching
07. Ruby String Slides - Ruby Core Teaching
quanhoangd129
 
03. Ruby Variables & Regex - Ruby Core Teaching
03. Ruby Variables & Regex - Ruby Core Teaching03. Ruby Variables & Regex - Ruby Core Teaching
03. Ruby Variables & Regex - Ruby Core Teaching
quanhoangd129
 
02. Ruby Basic slides - Ruby Core Teaching
02. Ruby Basic slides - Ruby Core Teaching02. Ruby Basic slides - Ruby Core Teaching
02. Ruby Basic slides - Ruby Core Teaching
quanhoangd129
 
06. Ruby Array & Hash - Ruby Core Teaching
06. Ruby Array & Hash - Ruby Core Teaching06. Ruby Array & Hash - Ruby Core Teaching
06. Ruby Array & Hash - Ruby Core Teaching
quanhoangd129
 
05. Ruby Control Structures - Ruby Core Teaching
05. Ruby Control Structures - Ruby Core Teaching05. Ruby Control Structures - Ruby Core Teaching
05. Ruby Control Structures - Ruby Core Teaching
quanhoangd129
 
08. Ruby Enumerable - Ruby Core Teaching
08. Ruby Enumerable - Ruby Core Teaching08. Ruby Enumerable - Ruby Core Teaching
08. Ruby Enumerable - Ruby Core Teaching
quanhoangd129
 
01. Ruby Introduction - Ruby Core Teaching
01. Ruby Introduction - Ruby Core Teaching01. Ruby Introduction - Ruby Core Teaching
01. Ruby Introduction - Ruby Core Teaching
quanhoangd129
 
09. Ruby Object Oriented Programming - Ruby Core Teaching
09. Ruby Object Oriented Programming - Ruby Core Teaching09. Ruby Object Oriented Programming - Ruby Core Teaching
09. Ruby Object Oriented Programming - Ruby Core Teaching
quanhoangd129
 
07. Ruby String Slides - Ruby Core Teaching
07. Ruby String Slides - Ruby Core Teaching07. Ruby String Slides - Ruby Core Teaching
07. Ruby String Slides - Ruby Core Teaching
quanhoangd129
 
03. Ruby Variables & Regex - Ruby Core Teaching
03. Ruby Variables & Regex - Ruby Core Teaching03. Ruby Variables & Regex - Ruby Core Teaching
03. Ruby Variables & Regex - Ruby Core Teaching
quanhoangd129
 
02. Ruby Basic slides - Ruby Core Teaching
02. Ruby Basic slides - Ruby Core Teaching02. Ruby Basic slides - Ruby Core Teaching
02. Ruby Basic slides - Ruby Core Teaching
quanhoangd129
 
06. Ruby Array & Hash - Ruby Core Teaching
06. Ruby Array & Hash - Ruby Core Teaching06. Ruby Array & Hash - Ruby Core Teaching
06. Ruby Array & Hash - Ruby Core Teaching
quanhoangd129
 
05. Ruby Control Structures - Ruby Core Teaching
05. Ruby Control Structures - Ruby Core Teaching05. Ruby Control Structures - Ruby Core Teaching
05. Ruby Control Structures - Ruby Core Teaching
quanhoangd129
 
08. Ruby Enumerable - Ruby Core Teaching
08. Ruby Enumerable - Ruby Core Teaching08. Ruby Enumerable - Ruby Core Teaching
08. Ruby Enumerable - Ruby Core Teaching
quanhoangd129
 
Ad

Recently uploaded (20)

How to Choose the Right Web Development Agency.pdf
How to Choose the Right Web Development Agency.pdfHow to Choose the Right Web Development Agency.pdf
How to Choose the Right Web Development Agency.pdf
Creative Fosters
 
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration KeySmadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
joybepari360
 
Looking for a BIRT Report Alternative Here’s Why Helical Insight Stands Out.pdf
Looking for a BIRT Report Alternative Here’s Why Helical Insight Stands Out.pdfLooking for a BIRT Report Alternative Here’s Why Helical Insight Stands Out.pdf
Looking for a BIRT Report Alternative Here’s Why Helical Insight Stands Out.pdf
Varsha Nayak
 
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Intelli grow
 
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWSWomen in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage OverlookCode and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
What is data visualization and how data visualization tool can help.pdf
What is data visualization and how data visualization tool can help.pdfWhat is data visualization and how data visualization tool can help.pdf
What is data visualization and how data visualization tool can help.pdf
Varsha Nayak
 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
 
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
Microsoft Business-230T01A-ENU-PowerPoint_01.pptxMicrosoft Business-230T01A-ENU-PowerPoint_01.pptx
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
soulamaabdoulaye128
 
Porting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 WebinarPorting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
 
Plooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your wayPlooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your way
Plooma
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptxIMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
Agile Software Engineering Methodologies
Agile Software Engineering MethodologiesAgile Software Engineering Methodologies
Agile Software Engineering Methodologies
Gaurav Sharma
 
How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0
Anchore
 
Advanced Token Development - Decentralized Innovation
Advanced Token Development - Decentralized InnovationAdvanced Token Development - Decentralized Innovation
Advanced Token Development - Decentralized Innovation
arohisinghas720
 
Generative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its ApplicationsGenerative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its Applications
SandeepKS52
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutionsZoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Alluxio, Inc.
 
Migrating to Azure Cosmos DB the Right Way
Migrating to Azure Cosmos DB the Right WayMigrating to Azure Cosmos DB the Right Way
Migrating to Azure Cosmos DB the Right Way
Alexander (Alex) Komyagin
 
How Insurance Policy Management Software Streamlines Operations
How Insurance Policy Management Software Streamlines OperationsHow Insurance Policy Management Software Streamlines Operations
How Insurance Policy Management Software Streamlines Operations
Insurance Tech Services
 
How to Choose the Right Web Development Agency.pdf
How to Choose the Right Web Development Agency.pdfHow to Choose the Right Web Development Agency.pdf
How to Choose the Right Web Development Agency.pdf
Creative Fosters
 
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration KeySmadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
joybepari360
 
Looking for a BIRT Report Alternative Here’s Why Helical Insight Stands Out.pdf
Looking for a BIRT Report Alternative Here’s Why Helical Insight Stands Out.pdfLooking for a BIRT Report Alternative Here’s Why Helical Insight Stands Out.pdf
Looking for a BIRT Report Alternative Here’s Why Helical Insight Stands Out.pdf
Varsha Nayak
 
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Intelli grow
 
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWSWomen in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage OverlookCode and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
What is data visualization and how data visualization tool can help.pdf
What is data visualization and how data visualization tool can help.pdfWhat is data visualization and how data visualization tool can help.pdf
What is data visualization and how data visualization tool can help.pdf
Varsha Nayak
 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
 
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
Microsoft Business-230T01A-ENU-PowerPoint_01.pptxMicrosoft Business-230T01A-ENU-PowerPoint_01.pptx
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
soulamaabdoulaye128
 
Porting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 WebinarPorting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
 
Plooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your wayPlooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your way
Plooma
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptxIMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
Agile Software Engineering Methodologies
Agile Software Engineering MethodologiesAgile Software Engineering Methodologies
Agile Software Engineering Methodologies
Gaurav Sharma
 
How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0
Anchore
 
Advanced Token Development - Decentralized Innovation
Advanced Token Development - Decentralized InnovationAdvanced Token Development - Decentralized Innovation
Advanced Token Development - Decentralized Innovation
arohisinghas720
 
Generative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its ApplicationsGenerative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its Applications
SandeepKS52
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutionsZoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Alluxio, Inc.
 
How Insurance Policy Management Software Streamlines Operations
How Insurance Policy Management Software Streamlines OperationsHow Insurance Policy Management Software Streamlines Operations
How Insurance Policy Management Software Streamlines Operations
Insurance Tech Services
 
Ad

04. Ruby Operators Slides - Ruby Core Teaching

  • 2. Contents Ruby Arithmetic Operators 1. Ruby Comparison Operators 2. Ruby Assignment Operators 3. Ruby Parallel Assignment 4. Ruby Bitwise Operators 5. Ruby Logical Operators 6. Ruby Ternary Operator 7. Ruby Range Operators 8. Ruby defined? Operators 9. Ruby Dot "." and Double Colon "::" Operators 10. Ruby Operators Precedence 11.
  • 4. Sr.No. Operator & Description Example 1 + Addition − Adds values on either side of the operator. a + b will give ? 2 − Subtraction − Subtracts right hand operand from left hand operand. a - b will give ? 3 * Multiplication − Multiplies values on either side of the operator. a * b will give ? 4 / Division − Divides left hand operand by right hand operand. b / a will give ? 5 % Modulus − Divides left hand operand by right hand operand and returns remainder. b % a will give ? 6 ** Exponent − Performs exponential (power) calculation on operators. a**b will give 10 to the power ?
  • 6. Sr.No. Operator & Description Example 1 == Checks if the value of two operands are equal or not, if yes then condition becomes true. (a == b) is not true. 2 != Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (a != b) is true. 3 > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (a > b) is not true. 4 < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (a < b) is true. 5 >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (a >= b) is not true.
  • 7. Sr.No. Operator & Description Example 6 <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (a <= b) is true. 7 ⇔ Combined comparison operator. Returns 0 if first operand equals second, 1 if first operand is greater than the second and -1 if first operand is less than the second. (a <=> b) returns -1. 8 === Used to test equality within a when clause of a casestatement. (1...10) === 5 returns true. 9 .eql? True if the receiver and argument have both the same type and equal values. 1 == 1.0 returns true, but 1.eql?(1.0) is false. 10 equal? True if the receiver and argument have the same object id. if aObj is duplicate of bObj then aObj == bObj is true, a.equal?bObj is false but a.equal?aObj is true
  • 9. Sr.No. Operator & Description Example 1 = Simple assignment operator, assigns values from right side operands to left side operand. c = a + b will assign the value of a + b into c 2 += Add AND assignment operator, adds right operand to the left operand and assign the result to left operand. c += a is equivalent to c = c + a 3 -= Subtract AND assignment operator, subtracts right operand from the left operand and assign the result to left operand. c -= a is equivalent to c = c - a 4 *= Multiply AND assignment operator, multiplies right operand with the left operand and assign the result to left operand. c *= a is equivalent to c = c * a
  • 10. Sr.No. Operator & Description Example 5 /= Divide AND assignment operator, divides left operand with the right operand and assign the result to left operand. c /= a is equivalent to c = c / a 6 %= Modulus AND assignment operator, takes modulus using two operands and assign the result to left operand. c %= a is equivalent to c = c % a 7 **= Exponent AND assignment operator, performs exponential (power) calculation on operators and assign value to the left operand. c **= a is equivalent to c = c ** a
  • 12. Parallel Assignment Ruby also supports the parallel assignment of variables. This enables multiple variables to be initialized with a single line of Ruby code
  • 13. Parallel Assignment Parallel assignment is also useful for swapping the values held in two variables −
  • 15. Bitwise Operators Bitwise operator works on bits and performs bit by bit operation. Assume if a = 60; and b = 13; now in binary format they will be as follows
  • 16. Sr.No. Operator & Description Example 1 & Binary AND Operator copies a bit to the result if it exists in both operands. (a & b) will give 12, which is 0000 1100 2 | Binary OR Operator copies a bit if it exists in either operand. (a | b) will give 61, which is 0011 1101 3 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (a ^ b) will give 49, which is 0011 0001
  • 17. Sr.No. Operator & Description Example 4 ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~a ) will give -61, which is 1100 0011 in 2's complement form due to a signed binary number. 5 << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. a << 2 will give 240, which is 1111 0000 6 >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. a >> 2 will give 15, which is 0000 1111
  • 19. Sr.No. Operator & Description Example 1 and Called Logical AND operator. If both the operands are true, then the condition becomes true. (a and b) is true. 2 or Called Logical OR Operator. If any of the two operands are non zero, then the condition becomes true. (a or b) is true. 3 && Called Logical AND operator. If both the operands are non zero, then the condition becomes true. (a && b) is true.
  • 20. Sr.No. Operator & Description Example 4 || Called Logical OR Operator. If any of the two operands are non zero, then the condition becomes true. (a || b) is true. 5 ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false. !(a && b) is false. 6 not Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false. not(a && b) is false.
  • 22. Sr.No. Operator & Description Example 1 ? : Conditional Expression If Condition is true ? Then value X : Otherwise value Y
  • 24. Sr.No. Operator & Description Example 1 .. Creates a range from start point to end point inclusive. 1..10 Creates a range from 1 to 10 inclusive. 2 ... Creates a range from start point to end point exclusive. 1...10 Creates a range from 1 to 9. Range Operators
  • 26. defined? Operators defined? is a special operator that takes the form of a method call to determine whether or not the passed expression is defined. It returns a description string of the expression, or nil if the expression isn't defined.
  • 27. Dot "." and Double Colon "::" Operators
  • 30. Method Operator Description && Logical 'AND' || Logical 'OR' .. ... Range (inclusive and exclusive) ? : Ternary if-then-else = %= { /= -= += |= &= >>= <<= *= &&= ||= **= Assignment defined? Check if specified symbol defined not Logical negation or and Logical composition