×

ruby · level 1 · unit 3 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. 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.