Ruby operators

Ruby operators perform arithmetic, compare values, test conditions, and assign results.

A simple example

Test two conditions

  1. Start with n is 2
  2. Do this Greater than 0 and less than 3?
  3. Result yes
Both comparisons succeed, so the program prints yes.
lesson.rb
n = 2
puts "yes" if n > 0 && n < 3
Output
yes
Shelf quantities: 2 and 5. Order needs: At least 5. Second shelf: 5 available.
Look up a shelf safely

A stock scanner receives a shelf position before it knows whether that shelf exists. Check the bounds first. Only an existing shelf with at least five units can supply the order.

Types of Operators

  1. Arithmetic operator
  2. Relational operator
  3. Logical operator
  4. Bitwise operator
  5. Assignment operator
  6. Range operator
  7. Dot and Colon operator
  8. Defined operator
  9. Ternary operator