Ruby Academy · Lesson

Arithmetic and Operators

Math operations.

Lesson 2 of 413 steps

Arithmetic and Operators is a free Ruby Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.

Basic Arithmetic

Ruby supports the familiar operators: +, -, *, and /.

puts 5 + 3
puts 5 - 3
puts 5 * 3
puts 6 / 3

The Modulo Operator

% returns the remainder of a division. It is great for checking divisibility.

puts 10 % 3
puts 10 % 2

Exponentiation

** raises a number to a power.

puts 2 ** 3
puts 5 ** 2

Operator Precedence

Multiplication and division happen before addition and subtraction. Use parentheses to control order.

puts 2 + 3 * 4
puts (2 + 3) * 4

Compound Assignment

Operators like += and *= update a variable in place.

total = 10
total += 5
total *= 2
puts total

Comparison Operators

Compare numbers with <, >, <=, >=, ==, and !=. They return true or false.

puts 5 > 3
puts 5 == 5
puts 5 != 4

The Spaceship Operator

<=> returns -1, 0, or 1 depending on order. It powers sorting.

puts (1 <=> 2)
puts (2 <=> 2)
puts (3 <=> 2)

divmod

divmod returns both the quotient and remainder in one call as an array.

q, r = 17.divmod(5)
puts q
puts r

Integer and Float Mixing

Mixing an integer and a float promotes the result to a float.

puts 5 + 2.0
puts 3 * 1.5

times and upto

Integers have iteration helpers. times repeats and upto counts up.

3.times { |i| print "#{i} " }
puts
1.upto(3) { |i| print "#{i} " }
puts

Building a Total

Combine operators in a small calculation.

price = 20
quantity = 3
tax = 0.1
subtotal = price * quantity
total = subtotal + subtotal * tax
puts total

Quick Check

Check arithmetic and operators.

Recap

You learned operators:

  • arithmetic + - * /, modulo %, power **
  • precedence and parentheses
  • compound assignment, comparisons, <=>, divmod
  • times and upto for iteration

Next: the Math module.

puts (10 % 3) + (2 ** 3)
Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “Arithmetic and Operators” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “Arithmetic and Operators”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.

What will I learn in “Arithmetic and Operators”?

Math operations. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Arithmetic and Operators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Integers and Floats
  2. Arithmetic and Operators
  3. The Math Module
  4. Rational and BigDecimal
← Back to Ruby Academy