Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,000 --> 00:00:07,680
Okay, welcome back. Now that you have Ruby installed, let's start writing some Ruby code.
2
00:00:07,680 --> 00:00:11,040
So there's two ways to go about this. If we want some instant feedback, we want to try
3
00:00:11,040 --> 00:00:15,380
some experimenting, try something out, we're going to use IRB. If we want to save a program
4
00:00:15,380 --> 00:00:20,800
file and run it later, we'll write a Ruby program file. So let's start an IRB.
5
00:00:20,800 --> 00:00:25,760
Okay, so to start IRB, we just use the IRB command, which ships with Ruby, so it was
6
00:00:25,760 --> 00:00:31,280
installed when you installed Ruby. Now IRB stands for the Ruby Interactive Shell, and
7
00:00:31,280 --> 00:00:36,200
it's what we call a read eval print loop. So it's going to read whatever we type in,
8
00:00:36,200 --> 00:00:41,360
evaluate it as Ruby code, print out the results so that we can get real quick feedback, and
9
00:00:41,360 --> 00:00:45,120
then it's just going to loop back around and ask us for more Ruby code. So let's give that
10
00:00:45,120 --> 00:00:49,960
a try. We'll fire up IRB, and we get a little prompt. We can just start to type any Ruby
11
00:00:49,960 --> 00:00:56,520
code. I'm just going to start with a basic Ruby string. Type in, Ruby is fun. And it evaluated
12
00:00:56,520 --> 00:01:00,680
that code, and then the evaluation of that code is a string, so it just prints the string
13
00:01:00,680 --> 00:01:05,620
right back out on the second line there. So let's go ahead and assign that to a variable.
14
00:01:05,620 --> 00:01:10,680
Now one time saver in IRB is you can use up and down arrows as command line histories.
15
00:01:10,680 --> 00:01:14,680
So if I use my up arrow here, I get the command back, and then I'm going to assign it to a
16
00:01:14,680 --> 00:01:19,560
variable called comment, and then it evaluates that. The evaluation of that is the string
17
00:01:19,560 --> 00:01:24,000
Ruby is fun. In fact, I can type in comment now. I've got that variable, and it's storing
18
00:01:24,000 --> 00:01:28,960
the string. And if I hit return, I've got my string all stored in a variable. So let's
19
00:01:28,960 --> 00:01:33,080
do something with the string. Let's say we wanted to up case it. We can just call the
20
00:01:33,080 --> 00:01:38,560
up case method on that variable, and we get back Ruby is fun, all shouted in big capital
21
00:01:38,560 --> 00:01:44,640
letters like that. So we can call methods inside of IRB. We can also use some of the
22
00:01:44,640 --> 00:01:48,800
classes that are in the built-in Ruby standard library. So for example, if I wanted to know
23
00:01:48,800 --> 00:01:53,840
what time it is, I could type in time.new. It's going to evaluate that, and it's going
24
00:01:53,840 --> 00:01:56,080
to give me the current time.
25
00:01:56,080 --> 00:02:00,080
Now let's show them a couple errors that they might run across while they're in IRB.
26
00:02:00,080 --> 00:02:04,160
Yeah, there are a couple pitfalls in IRB to be aware of. Let's say you started a string
27
00:02:04,160 --> 00:02:09,160
like Ruby is, and then you hit return before you ended it. Well, Ruby's just going to wait
28
00:02:09,160 --> 00:02:12,840
here for a few minutes, waiting for us to type in the end of the string so I could say
29
00:02:12,840 --> 00:02:17,740
fun, and now it prints that string. It's got a new line inside of it because I actually
30
00:02:17,740 --> 00:02:19,920
typed a new line character.
31
00:02:19,920 --> 00:02:24,920
Another one is maybe you start with the variable comment equals and hit return, and you see
32
00:02:24,920 --> 00:02:29,960
that little question mark prompt there, so then we can finish that off. Ruby is fun,
33
00:02:29,960 --> 00:02:34,800
and we get the same thing. Finally, you might just type in comment and leave out the E,
34
00:02:34,800 --> 00:02:40,360
for example, and you'll get an error. It just says undefined local variable or method comment,
35
00:02:40,360 --> 00:02:44,760
and because that's the last thing we typed in, this comment right here, well, sure enough,
36
00:02:44,760 --> 00:02:48,720
that's the error. If you see error message inside of IRB, just take a minute to read
37
00:02:48,720 --> 00:02:52,200
them. They're usually trying to help you out. Now, when you're all done in IRB, you can
38
00:02:52,200 --> 00:02:57,040
either type exit or whatever the end of file character is on your operating system. Control
39
00:02:57,040 --> 00:03:01,280
D would be that on the Mac here, so I'm just going to type exit, and I'm back out on a
40
00:03:01,280 --> 00:03:06,359
command prompt. Don't worry about variables and classes and methods for right now. We
41
00:03:06,359 --> 00:03:11,200
just wanted to show you how to use IRB and just do some basic Ruby inside of it.
42
00:03:11,200 --> 00:03:15,679
Now, IRB is great for experimenting, and in fact, in every exercise, we'll have you go
43
00:03:15,679 --> 00:03:19,920
and try some things and just give it a shot. Experiment around in IRB.
44
00:03:19,920 --> 00:03:24,160
Yeah, the only problem with IRB is we can't save all that code that we typed in and then
45
00:03:24,160 --> 00:03:28,239
rerun it. So to do that, we're going to have to create a Ruby program file.
46
00:03:28,239 --> 00:03:29,399
Let's do that next.
47
00:03:29,399 --> 00:03:33,040
Okay, so back here over on the terminal, I'm just going to clean things up a little bit
48
00:03:33,040 --> 00:03:38,200
here. I'm in the Ruby Studio directory that I created, and it's empty. There are no files
49
00:03:38,200 --> 00:03:42,720
inside of here, so we want to create a Ruby program file. Now, I'm going to use the TextMate
50
00:03:42,720 --> 00:03:46,640
editor, and it has a command mate that's just going to let me create a new file and open
51
00:03:46,640 --> 00:03:56,040
it up. And I'm going to call the file, helloRuby.rb. And then we got TextMate open, and now we
52
00:03:56,040 --> 00:03:57,959
can just start to type some Ruby code.
53
00:03:57,959 --> 00:04:04,359
Now in the IRB session, it automatically printed out the string. So if I had Ruby is fun, just
54
00:04:04,360 --> 00:04:08,840
like that, in IRB, it would evaluate it and would print it out. Inside of a Ruby program
55
00:04:08,840 --> 00:04:13,680
file, to print it out to the console, we have to use the command put s. And put s takes
56
00:04:13,680 --> 00:04:17,520
a string, and it's just going to print that string out to the console. It's also going
57
00:04:17,520 --> 00:04:23,000
to add a new line there. So I can save off the program file now, go back out to the command
58
00:04:23,000 --> 00:04:27,320
line, and then to run the file, I type Ruby. That was a command that was installed when
59
00:04:27,320 --> 00:04:32,200
you installed Ruby. And then the name of our file is helloRuby.rb. And sure enough, we
60
00:04:32,200 --> 00:04:34,280
get Ruby is fun printed.
61
00:04:34,280 --> 00:04:36,840
Let's also show them how to run it from within TextMate.
62
00:04:36,840 --> 00:04:40,000
Well, that's a good idea, because I'm going to do this throughout the course, because
63
00:04:40,000 --> 00:04:44,200
it's sometimes easier just to run it directly from inside of the editor, as opposed to jumping
64
00:04:44,200 --> 00:04:48,840
out to the terminal. So to run it inside of TextMate, and your text editor probably has
65
00:04:48,840 --> 00:04:53,679
some, maybe a different command, but most of them support some way to run Ruby. In TextMate,
66
00:04:53,679 --> 00:04:58,000
I just use command r, and then I'm going to increase that font so you can see it. And
67
00:04:58,000 --> 00:05:01,000
you see it printed, Ruby is fun right there.
68
00:05:01,000 --> 00:05:05,280
Similar to what we did in IRB, let's go ahead and assign that to a variable. We'll assign
69
00:05:05,280 --> 00:05:10,520
it to a variable called comment, and then we'll print out the upcase version of that
70
00:05:10,520 --> 00:05:15,800
comment. So I'll run it again in TextMate. Sure enough, we get Ruby is fun.
71
00:05:15,800 --> 00:05:19,920
So let's say we want to do that like three times. Well, we'll talk more about the times
72
00:05:19,920 --> 00:05:24,840
method a little bit later, but three times in Ruby, it takes what's called a block, which
73
00:05:24,840 --> 00:05:30,480
starts with a do and an end. And inside of that block, we're just going to print out
74
00:05:30,480 --> 00:05:36,840
the upcase again. So we should get it printed four times here. And sure enough.
75
00:05:36,840 --> 00:05:38,560
Ruby is four times as fun.
76
00:05:38,560 --> 00:05:42,240
It's four times the fun of any other programming language. How's that?
77
00:05:42,240 --> 00:05:45,720
Now don't worry about the syntax here. We're going to spend a whole bunch of time on blocks
78
00:05:45,720 --> 00:05:49,720
in this course, so we're just kind of trying to get you a feel for what a Ruby file looks
79
00:05:49,720 --> 00:05:50,720
like.
80
00:05:50,720 --> 00:05:54,360
You know, in IRB, we also showed them about the time class. So it's probably worth showing
81
00:05:54,360 --> 00:06:00,120
that we can print out the time here. We just do print as time.new, or put as time.new there.
82
00:06:00,120 --> 00:06:05,280
And now we've got the time printed at the end of our Ruby fun little party there. And
83
00:06:05,280 --> 00:06:10,400
then back out on the command line, of course, I can run helloruby.rb and we get the exact
84
00:06:10,400 --> 00:06:14,920
same output. So now that we have this saved in the file, we can run it anytime we want.
85
00:06:14,920 --> 00:06:20,000
Perfect. Now we looked at a few errors in IRB. Maybe we should look at those errors in our
86
00:06:20,000 --> 00:06:21,160
program file too.
87
00:06:21,160 --> 00:06:25,120
Yeah, let's see how to get out of sticky spots in our program file here. So we've got our
88
00:06:25,120 --> 00:06:30,040
program file. We've got this comment. What happens if we leave off that trailing string?
89
00:06:30,040 --> 00:06:33,720
We can already see based on the color coding here that something's sort of off. But if
90
00:06:33,720 --> 00:06:37,800
we go ahead and try to run that, we're going to get an error. There's an un-terminated
91
00:06:37,800 --> 00:06:43,240
string. So Ruby is reading to the end of the file trying to find that terminating quote.
92
00:06:43,240 --> 00:06:45,400
So we can just stick that back in.
93
00:06:45,400 --> 00:06:51,760
What if we say misspell comment right here and we run that? Well, we get the same error
94
00:06:51,760 --> 00:06:57,320
we got in IRB. Undefined local variable or method comment. And we see that it's on line
95
00:06:57,320 --> 00:07:03,880
two of our HelloRuby.rb file. So we can just go to line two here. Sure enough, we've got
96
00:07:03,880 --> 00:07:06,760
a misspelling. We'll just clean that up.
97
00:07:06,760 --> 00:07:12,280
And then finally, maybe we thought that there was a method on time called time.then. If
98
00:07:12,280 --> 00:07:16,400
we try to run that, we're going to get an undefined method then. And again, it shows
99
00:07:16,400 --> 00:07:22,159
us the line number. It's on line eight. So we can just go there and fix that up.
100
00:07:22,159 --> 00:07:24,539
Ruby really tries to help you with the errors.
101
00:07:24,540 --> 00:07:29,080
It does. It really pays to read the errors. Don't take them as something really scary.
102
00:07:29,080 --> 00:07:33,480
Take them as something very helpful. Check the line number. And then, you know, fix things
103
00:07:33,480 --> 00:07:34,480
up.
104
00:07:34,480 --> 00:07:37,560
Right. All right. So now it's your turn to write your first bit of Ruby code. You're
105
00:07:37,560 --> 00:07:42,040
actually going to write in both places, IRB, and then you'll make a Ruby program file.
106
00:07:42,040 --> 00:07:45,180
And then in the next section, we'll look at numbers and strings. And that'll help you
107
00:07:45,180 --> 00:07:47,760
be able to create the first player in the game.
108
00:07:47,760 --> 00:07:49,680
That's right. All right. See you back here.
109
00:07:49,680 --> 00:07:56,680
See you soon.
12422
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.