ruby · level 1 · unit 3 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Ruby is a calculator that never gets tired.
+ adds, - takes away, * multiplies and / divides. Each of these signs is an . As in school maths, * and / happen before + and -, unless you use brackets.
Watch out with /. Divide one integer by another and Ruby gives a whole number, dropping what is left: 7 / 2 is 3. A number with a decimal point is a . Write 7.0 / 2 to get 3.5.
% gives the remainder, what is left over after sharing into equal groups. Share 7 sweets between 2 friends and 1 is left, so 7 % 2 is 1.
puts 2 + 3 * 4
total = (2 + 3) * 4
puts total
puts 7 / 2
puts 7.0 / 2
puts 7 % 2 Mix an integer and a float and the answer is a float, so 3.0 * 2 is 6.0.
guess first
Ten shared by four. What will Ruby say?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
puts 10 / 4
your turn
Your turn. Share out the pizza.
Type how many pizzas you are ordering, then how many friends will share them (not 0). Each pizza has 8 slices. Run it to see the slices, each share and what is left over.
fix it
Seven cakes for two friends should be 3.5 each. Fix it!
- Run it. Why does Ruby show a whole number instead of 3.5?
- Both values are integers, so
/drops the part left over. One of them needs a decimal point. - Change line 1 to cakes = 7.0, then press run.
nailed it
you turned ruby into a calculator. :D
*and/come before+and-, unless there are brackets.- Integer divided by integer drops the leftover part.
%gives the remainder after sharing.
new words in your collection
- operator
- float
This lesson isn’t saved. A free account remembers every lesson you finish.