Arithmetic and Operators
Math operations.
Arithmetic and Operators is a free Ruby Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Basic Arithmetic
Ruby supports the familiar operators: +, -, *, and /.
puts 5 + 3
puts 5 - 3
puts 5 * 3
puts 6 / 3The Modulo Operator
% returns the remainder of a division. It is great for checking divisibility.
puts 10 % 3
puts 10 % 2Exponentiation
** raises a number to a power.
puts 2 ** 3
puts 5 ** 2Operator Precedence
Multiplication and division happen before addition and subtraction. Use parentheses to control order.
puts 2 + 3 * 4
puts (2 + 3) * 4Compound Assignment
Operators like += and *= update a variable in place.
total = 10
total += 5
total *= 2
puts totalComparison Operators
Compare numbers with <, >, <=, >=, ==, and !=. They return true or false.
puts 5 > 3
puts 5 == 5
puts 5 != 4The 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 rInteger and Float Mixing
Mixing an integer and a float promotes the result to a float.
puts 5 + 2.0
puts 3 * 1.5times and upto
Integers have iteration helpers. times repeats and upto counts up.
3.times { |i| print "#{i} " }
puts
1.upto(3) { |i| print "#{i} " }
putsBuilding a Total
Combine operators in a small calculation.
price = 20
quantity = 3
tax = 0.1
subtotal = price * quantity
total = subtotal + subtotal * tax
puts totalQuick Check
Check arithmetic and operators.
Recap
You learned operators:
- arithmetic
+ - * /, modulo%, power** - precedence and parentheses
- compound assignment, comparisons,
<=>,divmod timesanduptofor iteration
Next: the Math module.
puts (10 % 3) + (2 ** 3)Frequently asked questions
Is the “Arithmetic and Operators” lesson free?
Yes — the full text of “Arithmetic and Operators” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
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
- Integers and Floats
- Arithmetic and Operators
- The Math Module
- Rational and BigDecimal