Skip to: Content | Navigation | Site map

Programming Kit - Hackety Hack

  Posted: April, 26 2007

Hackety Hack is a programming starter kit. Even if you’ve never written code before, Hackety Hack wants to be the spark that gets you going. And, believe it or not, you can write genuinely useful stuff with it!


Download Hackety Hack for Windows

Lesson One: Asking and Saving

Approx. time needed: 20 minutes

First, unzip the Hackety Hack ZIP file. It should contain a folder named HacketyHack-000.b (or similiar), drag that to your desktop. Open up the folder and click on hacketyhack.exe to start the program.

Part A: Asking

Let’s start with a very simple program.

  • Click the Start a new program link.
  • In the large white box, type the following code exactly as written:

Go to Top of the page

   # Asking and saying
   name = ask(“Your name please?”)
   puts name
  • Click Run when you’re finished.

The code you’ve written is for a language called Ruby, which is the language spoken inside Hackety Hack.

Now, take a moment to look at the program. See if you can figure out how it works. What do you suppose the ask command does? And what is that name word for?

Now Save the program. Call it Lesson 1.

After that, click on the Hackety Hack logo to return to the main menu. (Or go to the File menu and click on Home.)

Notice that the program is listed under Saved Programs. You should see the name Lesson 1 and, underneath it, the short description Asking and saying. Well done!

A Note About Saving

In the first line of your program, you place a short comment. Comments start with the pound (or number) symbol. That criss-cross chex mix thing. Comments are ignored by Ruby.

However, Hackety Hack will check the beginning of your program for a comment. If one is found, it will be used as the description. Thank you so much Hackety Hack!!

Does this make sense?

Go to Top of the page

How Asking Works

The ask command you used in Part A pops up a window and lets the person at the computer type something in. Whatever they end up typing is handed back to your program. In Part A, you stored the typing in the name variable.

So how does Ruby know that ask is a command and name is a variable? Because of their position in the “sentences” of your code. For example, variables are created with an equals sign. Since name comes before the equals sign, it is a variable.

Go to Top of the page

name = ask(“Your name please?”)

Part of coding is simply just spotting the order of things.

EXTRA CREDIT: Try changing the question to something a bit meaner. Make your program testy for me!

Part B: Saying

Click on the name of your program Lesson 1 to go back to editing it. We’re going to do something else with that name.

Go to Top of the page

Here’s the new program (the first 2 lines are the same):

 

# Asking and saying
name = ask(“Your name please?”)
say(“Your name is #{name.length} letters long”)
sleep name.length

Type this one in carefully. You’ll notice in the quotes that there is a little pound sign and some curly braces. The curly braces are right above your square bracket keys (on a US keyboard.)

Click Save to save what you’ve done so far. Then, Run.

Watch that barbershop pole spin. Did you notice the words above it change? The say command changes the words right above that barbershop pole!

What’s it Good For?

The say command is nice, because you can tell the person at the computer what’s going on while they’re waiting. You can say sending e-mail or downloading mp3 or just crunching away… depending on what your program is doing. It’s a very simple thing, but hey.. this is only lesson 1!!

Also notice the sleep command. Since the spinning pole goes away when your program’s finished, the sleep command lets us pause the program so you can see it for a few seconds before it goes away.

We’ll skip explanation of the other stuff for now, but run the program a few more times and see if you can tell for yourself:

  • What does name.length do?
  • What’s that thing surrounding it? #{….}

Part C: Counting

Okay, again: keep the first two lines of the program and go like this:

Go to Top of the page

# Asking and saying
name = ask(“Your name please?”)
1.upto(name.length) do |x|
  say “Your name has #{x} letters.”
  sleep 1
end
sleep 10

Notice the pipe symbols in the program. At the end of the third line, you’ll see |x|. Those two lines around the x are pipe symbols. They are like a little waterslide that x is shooting down. The pipe symbol is right above your Enter key. Press shift and that key and you’ll see a pipe.

The upto command

Just like ask and say are commands, so also is upto a command. But why does it have a dot before it? Why is it attached to the number 1?

Every command has a front and a back. The front of a command can attach to something with a dot. The back of a command can attach to something with parentheses.

Go to Top of the page

1.upto(6) do |x|
 say(“Holding up #{x} fingers”)
 sleep 1
end

The upto command is a part of every number. In this case, it is a part of the number 1. Think of it as a vicious dog that has a leash to its owner. The dot is the leash.

The parentheses are the dog’s mouth, eating the number six. The thing to remember is: commands are owned by whatevers on the left-side of the dot. And its gobbling up everything in the parentheses. This gives the command energy to do its job!! You know: Scooby Snacks.

Go to Top of the page

“Hello?”.upto(6)

See, that won’t work. Because quoted things don’t own a dog called upto. Because upto is for counting!!

We’re counting from 1 _up to_ 6. Nice and easy to read.

The End of Lesson 1

At this point, you probably have a lot of questions and you may feel that there’s a lot of unexplained stuff in there. Well, of course there is!! You can’t just know everything right away. But you have learned some things:

  • Commands do things, just like dogs do things.
  • Variables store things.
  • You create a variable with an equals sign. That’s how Ruby knows — by that equals sign!
  • Commands are owned by the thing on the left-side of the dot.
  • Commands eat useful things in parentheses, as if they were biscuits.
  • The commands ask, say, sleep and upto were explored a bit.

That’s pretty good, actually. Take a rest, enjoy your accolades and let your brain soak in what you’ve got so far.

EXTRA CREDIT: If you want to try stretching your brain a bit further, try writing a program which blinks. You can use the say and sleep commands to do it!!

Go to Top of the page

Source: HacketyHack

 

Related Articles

No Comments Yet

You can be the first to comment!

Leave a comment

*
To prove you're a person (not a spam script), type the security text shown in the picture.
Click here to regenerate some new text.

Click to hear an audio file of the anti-spam word