An important note: These lessons are now updated and built into Hackety Hack. Don't bother reading them here on the wiki. Just run HH and click on the lightbulb icon in the toolbar. Much obliged! - _why
Lesson Two: Getting Outdoors
Approx. time needed: 30 minutes Due date: Friday, Mar. 9th
In Lesson 1, we asked some things and we said somethings. Not bad, not bad. But not incredibly useful, you know? How about let's make the computer do something?
Specifically: let's get outdoors and start writing code that can connect to the Web.
Part A: Downloading
Start a new program.
# A simple file downloader.
url = ask("Enter a Web address:")
save_as = ask("Save the file as:")
Web.download(url, save_as)
Save the program as "Lesson 2". Then, click "Run".
This program asks twice. In the first "ask" box, type in an address to some file on the Web. How about the Dead Hensons' rendition of the "Pinball Number Count"?
Then, in the second box, type in a name for the downloaded file. Like:
pinball.mp3
What did the program do? Can you tell just by looking at the code?
Where Did the File Go?
So, you saw the file downloading, right? Now to find where it downloaded. Hackety Hack keeps your downloaded files in a folder, right next to where your programs are saved. Let's go dig it up!
C:\Documents and Settings\YOU\Application Data\Hackety Hack
Go to the folder above in Windows Explorer, replacing the word YOU with your Windows username. You can copy-and-paste it straight into the Explorer bar. Or start at "My Computer" and click down through each folder.
You should see a group of files ending in .rb, as well as a folder marked "Downloads". Aha! Each of the .rb files is one of your programs. These are genuine Ruby programs you can pass around to other Hackety Hackers. If a friend gives you a script, you can copy it into this folder and it will appear in Hackety Hack!
Now, check the "Downloads" folder. Well, well, well. Fancy that.
The Web Library
To download the MP3, we use the "Web.download" command. This is just another command like "ask" and "say", except that it is a part of Hackety Hack's Web library of commands. (In Ruby, these commands are also called "methods." Just so you know.)
To keep things organized, some commands are kept in libraries. Stuff like Web.fetch, Web.page and Web.download all happen to relate to working with the Internet, so they are kept in the "Web" library.
In upcoming lessons, you'll learn how to add your own libraries and how to use libraries from other hackers. This opens Hackety Hack up to new commands that don't come with it.
EXTRA CREDIT: Hackety Hack comes with a command "ask_save_file()" which is a special kind of "ask". See if you can change the third line of your program to use this command.
Part B: Fetching (Not Saving!)
While we're in the Web library, let's play with Web.fetch. I mean you basically know everything there is to know about Web.download. It downloads files from the Internet and saves them. THE END.
But Web.fetch is a bit smarter. It grabs things off the Internet and, rather than saving, lets you play with the thing a bit.
Here's the next example. Save this one as "Lesson 2a".
# Read the Metafilter feed.
puts Web.fetch("http://xml.metafilter.com/rss.xml")
Run the script. You'll see Hackety Hack download a file... but something else happens... hmmm!
Metafilter is a blog. So this example could work with any blog you like. The Web address we're using here is Metafilter's news feed. (Often sites will call this RSS or XML. Often, blogs will have a link in a sidebar or something.)
Playing With Fetch
Okay, so we've got Metafilter's feed with all the latest news posted on the blog. So, then what? What good is this? Can't you just go STRAIGHT TO METAFILTER.COM IN A REGULAR BROWSER??
Let's add a few lines to our program:
# Read the Metafilter feed.
feed = Web.fetch("http://xml.metafilter.com/rss.xml")
feed.items.reject! do |item|
item.description.length > 200
end
puts feed
Okay, let's step through this program. It's quite a bit more complicated, isn't it? But it's only five lines, so I think we can figure it out.
The Metafilter news gets saved in a variable called "feed". So we can play with it. In line 3, the "reject!" command is used. This command deletes things from a list. And the news feed contains a list of news "item"s. Hmmm.
Try this:
# Read the Metafilter feed.
feed = Web.fetch("http://xml.metafilter.com/rss.xml")
puts feed.items
When you run this, the "items" will be listed as "An Array" which is "a list of things".
Quickly, About Arrays
You can create arrays (aka "lists") with square brackets.
Try this little program. (You can hit Ctrl-Z to undo your changes one-by-one and Ctrl-Y to redo those changes again.)
puts ["HELLO", 1, 2, 3]
As you can see, you put square brackets on each end. And commas between each item. Like a caterpillar stapled to the screen. The square brackets are the staples and the commas are the caterpillar's legs.
Back to Reject!
Now that we know that a blog feed is just an ordinary list of things, we can start treating it like one. The "reject!" method will delete stuff from the list that we don't want. It has an exclamation point BECAUSE IT'S DANGEROUS!! Kablewy.
# Read the Metafilter feed.
feed = Web.fetch("http://xml.metafilter.com/rss.xml")
feed.items.reject! do |item|
item.description.length > 200
end
puts feed
This program will remove any thing from the list which is longer than 200 letters. We just want to focus on the short little Metafilter posts that show up sometimes. We're not in the mood for anything too long.
AH! So this is pretty handy, huh? If you like blogs, this is just the start. You could write programs to watch your favorite blogs for certain words. You could write a program to only show items containing MP3s. And we'll get into some other fancy things soon enough.
About the Do and End
We're going to skip getting too detailed on the "do"..."end" part, except to say: it checks to find blog entries longer than 200 letters. But we saw this in the last lesson, when we counted "upto":
1.upto(6) do |x|
say("Holding up #{x} fingers")
sleep 1
end
This "do"..."end" section is called a "block". It's usually slightly indented, but that's not necessary at all. Indenting the block makes it stand out a bit more, to help us see it.
The End of Lesson 2
Well, that was thrilling! You moved right off into doing some useful things. We started to grab stuff off the Web: downloading files and fetching blogs.
Here's a few programming bits you've picked up:
- The Web library contains commands for horsing around with the Internet.
- Web.download saves files off the web.
- They get stored in your Hackety Hack folder.
- Your programs are saved in that folder, too!
- Web.fetch can grab blog feeds and web pages.
- You can dissect a feed and change it or get stuff out of it.
- Web feeds are just a list of items.
- A list is also called an Array.
Very, very good. I do say. Take a break, eat a gigantic Dagwood sandwich, and please return when you are duly refreshed.
_why
