Friday, October 19, 2012

Teaching Ruby to a 9-Year-Old Part 3: Let's Program a Robot

This is part of a series of posts titled "Teaching Ruby to a 9-Year-Old."  See the Introduction for more information.



Computers Need to be Taught



Computers are giant calculators, and they can do billions of math problems every second.  They're also good at doing the same thing over and over.  But remember, they speak Computer Talk, not English.  And they have to be taught to do everything.  This can be a lot of work, but it's worth it in the end.

With Ruby, you can tell the computer to make decisions and do things.  We need to be careful how we tell it, though, because of the way computers think.  Let's look at a couple examples.

When Mom and Dad tell you, "Go put on your shoes for school," what are they really telling you to do?  In our house, it means:

  1. Put on your backpack
  2. Grab your water bottle
  3. Go out into the garage
  4. Put on your shoes
You know this because Mom and Dad taught you that.  (Sometimes Mom and Dad will remind you, and they'll even tell you all of those steps one by one!)  Remember, computers are dumb and have to be told everything.  Everything!  



Programming a Robot



So let's pretend we have a robot, which is really just a computer shaped like a human, and let's teach it how to do things.

Let's say that the robot already knows what things are.  It just doesn't know how to do stuff yet.  In Ruby, we'll put a list of actions together and define that list as something called a function.  (Define means to explain what something means.  For example, I just defined the word define.  Get it?)


Defining "Put on shoes for school" in Ruby

Now we've taught our robot something:  "If I say put_on_shoes_for_school, then grab_backpack, grab_water_bottle, go_into_garage, place_shoes_on_feet, that's it."

Notice that there are no spaces in the actions.  Also notice that you say "that's it" by writing "end".

If you programmed this into your robot, and then you gave your robot a command:  "put_on_shoes_for_school", what do you think the robot would do?  Let's try it using Ruby!

Step 1 - Defining function put_shoes_on_for_school


Okay, this part worked great.  Ruby gave us a fist-bump.  Let's tell it to put_on_shoes_for_school now.


Uh oh!  Why did Ruby barf?

Oh no!  It looks like Ruby didn't like what you said.  What do you think happened?

Remember how we learned that computers have to be told how to do everything?  Let's look closer at what we told it to do.  When we said put_on_shoes_for_school, Ruby said, "Okay, I'll start by grab_backpack.  Oh no!  I don't know how to grab_backpack!  I'm going to barf now!"

See?  We gave Ruby a list of things to do, but we didn't tell it how to do all of those things.  Make sense?  Here's what a more complete program might look like:


So now, if you told the robot to grab_backpack, grab_water_bottle, or go_into_garage, it knows what to do!

Oh my gosh, do you see what just happened?  Somehow you've ended up learning about a whole page of computer code!  If you showed this code to most grownups, they'd tell you it's too hard to figure out.  Just tell them all you're doing is defining some functions, duh!  We haven't even done the hard stuff yet!

Friday, October 12, 2012

Teaching Ruby to a 9-Year-Old, Part 2: The Smart - Dumb Computer

This is part of a series of posts titled "Teaching Ruby to a 9-Year-Old."  See the Introduction for more information.


Computers are Really Smart...

Computers are basically giant calculators.  Type in a big multiplication problem, and watch how long it takes to figure out the answer.  Note:  On computers, we say "times" with a *.


This took less than 1 second.  My computer is super smart!


Computers are Also Really Stupid and Stuck Up.

Computers speak their own special language.  If you say something to the computer, and you don't say it exactly the right way, it says it doesn't understand.  Then it barfs on you. Look at these examples when I try to tell it to clear the screen:




Don't Make the Computer Barf

When we write a computer program, we need to be sure we don't make the computer sick.  This means we have to be very careful about how we write things.  Even something as simple as forgetting to put the period at the end of a sentence can make the entire program not work.

 

Nil


A long time ago, I put Ruby on my tablet.  I thought it was broken because it kept saying nil all the time.  Later on, I learned that nil is good!

The word "nil" means "nothing."  When Ruby tells you "nil," it means "Nothing is wrong.  I'm not going to barf on you."  So whenever you see that word on your screen, get excited because the computer just gave you a high-five (or fist bump)!