Ruby Operators

christian.dinh's avatar
Published Jul 27, 2021Updated Sep 9, 2021
Contribute to Docs
  • Learn to program in Ruby, a flexible and beginner-friendly language used to create sites like Codecademy.
    • Beginner Friendly.
      9 hours
  • Learn the basics of building applications with this convenient and powerful web development framework.
    • With Certificate
    • Intermediate.
      6 hours

Arithmetic Operators

Basic math operations can be applied to integer and floating point values:

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % modulo (yields the remainder)
a = 30;
b = 20;
puts a + b
# Output: 50
puts a - b
# Output: 10
puts a * b
# Output: 600
puts a / b
# Output: 1.5
puts a % b
# Output: 10

Comparison Operators

Comparison operators can be used to compare two values:

  • > greater than
  • < less than
  • >= greater than or equal to
  • <= less than or equal to
  • == equal to
  • != not equal to

The result of a comparison is a boolean value true or false:

a = 5;
b = 3;
puts a > b;
# Output: true

All contributors

Contribute to Docs

Learn Ruby on Codecademy

  • Learn to program in Ruby, a flexible and beginner-friendly language used to create sites like Codecademy.
    • Beginner Friendly.
      9 hours
  • Learn the basics of building applications with this convenient and powerful web development framework.
    • With Certificate
    • Intermediate.
      6 hours