This is just a skeleton of the actual lesson. Content is added as it is edited and polished.
Let’s step back and ask a basic question: How do we download content from URLs?
It’s something we do so automatically – we simply enter the URL into our browser and hit Enter – that we don’t think about it (many Web users don’t even realize that the browser is downloading something every time you visit a web address).
But there’s no address bar in the Ruby programming environment. We just have text files and the blinking cursor of irb to work with.
So where does the URL go?
Revisiting irb
Break out with Ctrl-C
Your first efforts at typing in code may result in what seems like being thrown into limbo where the irb prompt seemingly refuses to do anything. In these cases, hit Ctrl-C a few times and hit Enter again, and that should break you out of the limbo loop.
And if irb really seems to be going haywire, make sure you’re actually in irb and not at your system’s command prompt. They both look about the same.
For the first part of this course, we will be programming in the Interactive Ruby console. If you remember it from the setup guide, it’s the box with the blinking cursor.
Follow these instructions:
- Open up your Terminal or Command Line
- Open up the Interactive Ruby Shell by typing
irb
and pressing Enter - Type a few numbers, hit Enter; the irb prompt should echo those numbers back.
In irb, the Ruby interpreter reads your input every time you hit Enter.
So start off by typing some numbers:
1 2 3 4 5 6 7 8 |
|
As you can see, the Ruby interpreter accepts input at every press of Enter. All we’re doing is giving it numbers so all it does in response is repeat those numbers.
The problem with URLs and other non-numbers
So let’s try the same thing with a URL:
1 2 3 |
|
That didn’t go well. None of the above should make sense to you, though the appearance of irb indicates that irb has a problem. And syntax error
is pretty clear. And if you really have a good idea for detail, the 8
refers to the 008
, which is the line number of the user input that caused the error.
But the only thing that matters is that the Ruby interpreter doesn’t like URLs.
OK, but it seemed to like the phrase Hello world
from our very first program. Type in that exact phrase into irb:
1 2 3 |
|
Doh.
Command words and “normal” words
If you go back to the installation chapter where we ran the Hello world
program, you’ll see that we didn’t just type Hello world
:
1
|
|
If you copy-and-paste that into irb, everything should go smoothly:
1 2 3 |
|
So the difference, besides that word puts
which we’ll get to later, is the quotation marks:
1 2 3 4 5 6 7 8 |
|
The word puts
, without quotation marks, seems to be some kind of command:
1 2 3 4 5 6 7 |
|
(I’ll cut to the chase: it stands for ‘print to screen’. If you enter it in all by itself, nothing is printed to screen.)
So think of it this way: There are obviously words in the Ruby language, such as puts
, that do something. And it doesn’t seem likely that every word you type into irb could refer to an actual Ruby command.
So how can the Ruby interpreter tell the difference between command words, such as puts
, and words that you want it to take in literally, like http://www.google.com/
? Or, even if you want to use puts
just as a literal word, not a Ruby command?
Quotation marks
(todo)