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 Three / Making a Blog
Approx. Time Needed: 20 minutes
Due Date: Thursday, March 12th
So, you're still trying to get comfortable with Ruby, but we've seen a lot more of what Hackety Hack can do. Downloading and eating blogs, not bad.
But this lesson's going to hopscotch all that other stuff. In this lesson, you'll learn to make your own blog in just a few lines!! And we'll start to figure out what all the little pieces are: strings, numbers, blocks. Stuff like that.
Part A: Web.popup
Start up another new program. Save this one as "Lesson 3":
# A simple popup
Web.popup do
title("My Page")
p("This belongs to ____")
end
You can put your name in where the blanks are. Save it. Run it. To close it, click on the "X" or the darkened area.
We're using a few things we've seen before, but let's go over each of them, to be clear.
Modules and methods
We've talked about modules. What's the module in the above code? Hint: only one is used.
And we've talked about commands (also called "methods".) What are the methods in the above code?
The module is the Web module. It contains a method called popup that we want to use. There are three methods in all: Web.popup, title and p.
The p method means "paragraph" here.
Blocks
Attached to Web.popup is a block.
Web.popup do
...
end
The block is everything between do and end. A block is a group of code attached to a command. Again: a GROUP OF CODE attached to a method.
A block is like an a new lung. You plug it in to make a method breathe. The Web.popup method gives its block one puff of air. And the block uses that puff of air to make all the parts that go inside the popup. One popup, made in one puff of air.
We used a block a few other times. Here's one:
1.upto(6) do |x|
say("Holding up #{x} fingers")
sleep 1
end
In this one, the block gets six puffs of air. Every time we count a new number, the block gets a puff. And it uses that puff to say something and take a short nap.
Puff. Puff. Puff. Puff. Puff. Puff! And after that sixth one, the block's life is over.
Words in Strings
Okay, so we've almost finished taking this apart:
# A simple popup
Web.popup do
title("My Page")
p("This belongs to ____")
end
First, we have the Web.popup method from the Web module, which is attached to a block, an iron lung that gets turned on and handed a puff of air. In the block, the title and p commands run.
Let's talk about the quoted phrases.
"My Page"
This is a string. It's a short string of letters. When you think "string", just imagine each letter is clipped to a clothesline. The little quotes are the clips on each end, hooked on to the clothesline.
Strings are great for messing with words. Try changing the third line to:
title("My Page".upcase().reverse())
And run it. And think about that.
What is title? And what are upcase and reverse??
Part B: A Blog Popup
Let's change the insides of the popup a bit. Instead, we want to add a new blog entry.
# Write in my blog
Web.popup do
title("Add a blog entry")
editline("Title")
editbox()
buttons { cancel; save }
end
Save the program. Run. Hey, neat! Try typing in a short blog entry and saving.
Hmm. Actually. That didn't save anywhere. It's gone!! You lost your precious blog entry!! That is unacceptable.
Let's change the second line to:
Web.popup('MyBlog') do
That tells Hackety Hack to attach the popup to the MyBlog table. Try running it again and typing in a blog entry. How'd that go??
While you're at it, save a few more blog entries. Get it going.
About Tables
Tables are empty boxes in Hackety Hack where you can store things. Each box will contain the same kinds of things. So in the MyBlog box, we'll be storing blog entries.
What's a blog entry? Well, in the popup we have an editline for the title and an editbox for typing the main blog paragraphs. So, based on the popup we made, a blog entry is a title and a bunch of paragraphs.
Tables are a great place to stuff links or journal entries or pictures or... anything really. And links could go in a "MyLinks" table and journals in a "ClaspedDiary" table... You know?
Part C: Reading the Blog
Go start a new program.
# Read my blog
blog = Table('MyBlog').recent(10)
puts blog
Save this one as "Lesson 2C". And run it, of course!!
The Table('MyBlog') opens up the the blog table you've been adding to. And the recent method grabs a certain number of the latest blog entries. In this case: ten entries.
Oh, also worth trying:
# Read my blog
blog = Table('MyBlog').recent(10)
Web.popup do
blog
end
What else can you do with tables? Well, we'll get into that. (And hopefully I can finish the Table help page soon...)
For now, have a rest. The next lesson will be short, but you deserve a break after making your own super special blog.
_why
