Lesson content is currently in draft form.
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
For the first part of this course, we will be programming in the Interactive Ruby shell. 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 |
|
(Note: The info before the prompt, 1.9.3p194 ... >
, is system information and may be different for you. The only thing to worry about now is what’s on the right of the >
)
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.
Break out with Ctrl-C:
Your first efforts at typing in code may result in being thrown in what seems like limbo: 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.
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 we want it to take in literally, like http://www.google.com/
? Or, even if we want to use puts
just as a literal word, not a Ruby command?
Quotation marks
Quotation marks in written English denote dialogue:
“Wherever smart people work, doors are unlocked.”
They also denote something special in programming:
1 2 |
|
Try pasting the URL at the beginning of the lesson into irb
1
|
|
No errors! Congrats, we’ve just discovered the String, one of Ruby’s basic datatypes (and for most other programming languages).
The String Class
Strings are sequences – i.e. strings – of characters, which can include a single letter, words, numbers, or the entire text of Shakespeare.
Here are some examples of strings:
1 2 3 4 5 |
|
The quotation marks are the symbols that set off the string. When the Ruby interpreter sees the first quotation mark, it knows that we are creating a string. Everything after that quotation mark is considered a string. When the second quotation mark is reached, the Ruby interpreter knows the string is done:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Single quote marks
Either double-quote or single-quote marks can be used to create strings:
1 2 3 |
|
Note that in the second example, a single-quote mark was treated as a literal character (i.e. with no special meaning by the Ruby interpreter) because the string is enclosed in double-quote marks.
The upshot of this – and a common cause of error and confusion for beginners – is that the same type of quote-mark that opens a string must be used to close it.
In irb, we’re used to the Ruby interpreter responding with feedback after we press Enter. However, if we don’t close the string properly when hitting Enter, the Ruby interpreter will assume that the pressing of Enter means we want a new line. Because inside a string, a new line is just another character:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
For simplicity sake, I recommend using double-quotes at all times during the scope of this tutorial.
Exercise: Fix the broken quotes
Knowing the above pitfall above, fix the broken strings on each line below so that each line is its own self-contained string:
1 2 3 |
|
What have we learned so far? Just how to get the Ruby interpreter to read URLs as strings. Think of it as that we now know the method of entry for data. Nothing has happened yet (other than the string being echoed back).