HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RubyVariablesData TypesOperatorsStringsArraysHash (Dictionary)Conditions (if / else)LoopsMethods (def)Classes
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🗂️ Hash (Dictionary)
🔁 Condition & Loop
🧩 Method & Class
Ruby›Operators

Operators

Math (+ - * / % **), comparison (== < >), and logic (&& || !).

Simple enough for a 5-year-old to understand

Math

Example code
1
puts 7 % 2
Remainder: 1
CodeRemainder: 1
2
puts 2 ** 10
Exponent: 1024
CodeExponent: 1024
3
puts 7 / 2
Integer division: 3
CodeInteger division: 3
4
puts 7.0 / 2
Decimal division: 3.5
CodeDecimal division: 3.5
Code
💡 Hint: Integer division truncates the decimal: 7 / 2 = 3. For decimals, write 7.0 / 2.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the remainder of dividing 23 by 4.

🎯 Expected output: 3

Loading editor…

About this topic

Most operators in Ruby are actually METHODS. When you write a plus sign, the Integer class's addition method is called. This is a result of the language's consistency and allows you to define operator behavior for your own class.

Ruby's comparison operators involve three distinct concepts: value equality, object identity, and pattern matching. Double equals compares values; the equal? method asks if they are the exact same object. Confusing the two leads to silent bugs.

Ruby's safe navigation operator prevents errors when calling a method on nil: if the object is nil, the method is not called and the result is nil. Calling a method on nil is the most common runtime error in Ruby.

What you will learn

  • Arithmetic and comparison operators
  • That operators are methods
  • The difference between value equality and object identity
  • The safe navigation operator

When to use?

The safe navigation operator is used in chains where a value might be nil; otherwise, a method call on nil stops the program.

If you are defining comparisons in your own classes, writing the spaceship operator makes sorting methods work.

Common mistakes

Wrong

name = nil
puts name.upcase

Methods cannot be called on nil; the interpreter gives the error "undefined method 'upcase' for nil". This is the most common runtime error in Ruby. The safe navigation operator does not call the method if the object is nil and returns nil.

Correct

name = nil
puts name&.upcase.to_s

Wrong

a = "test"
b = "test"
if a.equal?(b)
  puts "Same"
end

The equal? method asks if they are the exact same OBJECT, not comparing contents. Since two strings are separate objects, the result is false. Double equals is used for content comparison; equal? is only used when identity is needed.

Correct

a = "test"
b = "test"
if a == b
  puts "Content is the same"
end

Wrong

count = 5
count++
puts count

There is NO increment operator in Ruby; the interpreter will throw a syntax error. Instead, compound assignment is used. The reason for this omission is that everything in Ruby is an object and integers are immutable.

Correct

count = 5
count += 1
puts count

Frequently asked questions

Are operators really methods?

Yes, most of them. You can also call the plus sign using dot notation and the result is the same. The practical implication of this is that if you define an addition method in your own class, instances of that class can be added together using the plus sign.

Is there a ternary operator?

Yes, it exists and works, but because if is an expression in Ruby and produces a value, a single-line if is considered more readable. The postfix condition syntax does the exact same thing and is widely used in the Ruby community.

How does the modulo operation behave with negative numbers?

The sign of the result is the same as the sign of the RIGHT operand, meaning minus seven modulo three results in two. In C and Java, the same operation yields minus one. For circular index calculations, Ruby's behavior usually gives you the result you want.

Summary

Most operators are methods and can be defined for your own class.

Double equals compares the value, while equal? compares the object identity.

There is no increment operator; compound assignment is used instead.

The safe navigation operator prevents method call errors on nil.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code