Teaching an 8 year old to code with Ruby

Here’s a very simple Ruby script I put together this evening for my daughter. She’s just started a book called Computer Coding for Kids and the first little Python script in there is a game where you are being chased by a ghost and have to guess which door to open. If the door you open has no ghost behind it, you get to continue to the next room and go again. If there is a ghost, the game ends.

After she got the code working, we discussed how we could use the same idea, but have a different story line to the game. Because it’s Halloween, she thought it would be great to pretend we were trick or treating and have to guess what kind of treat she would get each time she rang on a doorbell.

I put this script together for her to copy. I think it’s a good starting point for kids who are already confident with technology and who have perhaps played with things like scratch before and maybe have some concept of syntax.

guess = nil
trick_or_treat = nil
puts "It's Halloween"
puts "."
puts "."
puts "."
puts "And you're out trick or treating."
3.times { puts "." }
until guess != trick_or_treat do
  puts "You walk toward a house with a pumpkin outside and ring the bell."
  puts "The door creaks open and you shout 'Trick Or Treat?'"
  puts "What type of treat will you get?"
  print "Enter 1 for lollipop, 2 for chocolate bar, 3 for toffee apple: "
  trick_or_treat = 1 + rand(2)
  guess = gets.chomp.to_i
  if guess == trick_or_treat
    puts "Yay! you guessed correctly. That's another sweet in the bag."
  end
end
puts "Bad luck, you you guessed wrong, time to go home."

There’s quite a few concepts in this script that I threw in there to sort of lead my daughter’s learning. She’s done some basic programming in a few visual programming languages and a little Logo. She’s also done some introductions at school where they explained what an algorithm is and they did some very simple logic like comparing two numbers and drawing out some if conditions.

Here are the interesting (at least to us) points of discussion. When I’m doing this sort of teaching, I generally try not to answer for her unless it’s clear that she really has no idea what’s going on. In that case, I’ll usually take a step back and try to explain the concept in a different way and come back to the actual question I’ve posed another time to see if it “clicks”. This usually makes the learning a little more fun. It’s a game of discovery. Though, of course, takes longer than just shoveling information in 🙂

Anyway, on to the questions (note she didn’t necessarily get all of these – one or two I listened to her reasoning and then shelved until another day – especially the points on nil and variable initialisation):

* How many ways were there in the code to print three full stops? Do you think there could be other ways? If you could make up a computer language, how would you make the computer print three full stops?
* What is `nil`? Why is it there? Why are those two lines there setting variables to nil?
* What are variables anyway?
* Given the way the code works, what does `!=` mean? Here I prompted her a bit more – tell me what the code does in your own words…..etc
* What do you think `gets` does? (we ignored `chomp` and `to_i` for today)
* Why are there two equals signs for comparison