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,480
Alright, now in the last exercise you've established your first player.
2
00:00:07,480 --> 00:00:11,440
You used one variable to hold its name and another to hold its health.
3
00:00:11,440 --> 00:00:14,200
But we're going to need more than one player in our game.
4
00:00:14,200 --> 00:00:15,360
Yeah, okay.
5
00:00:15,360 --> 00:00:19,500
So that also means that we're going to need to use more variables.
6
00:00:19,500 --> 00:00:24,360
In the upcoming exercise, your objective will be to create more players and print them out
7
00:00:24,360 --> 00:00:26,480
so it'll look something like this.
8
00:00:26,480 --> 00:00:29,300
But again, we don't want to spoil the fun for you.
9
00:00:29,300 --> 00:00:33,180
So we're going to continue with our movie theme and leave the game to you.
10
00:00:33,180 --> 00:00:39,200
So our objective will be to create more movies and print them out like this using variables.
11
00:00:39,200 --> 00:00:42,480
Now understanding variables and how they work is really important.
12
00:00:42,480 --> 00:00:47,280
So let's take a moment just to step back and look at variables in a bit more depth.
13
00:00:47,280 --> 00:00:51,840
This will also give us an opportunity to start looking at calling methods on objects.
14
00:00:51,840 --> 00:00:55,800
Okay, so let's hop into an IRB session again.
15
00:00:55,800 --> 00:00:57,280
And we need a variable here.
16
00:00:57,280 --> 00:00:58,580
So let's create a variable movie.
17
00:00:58,580 --> 00:01:01,519
We're going to use our old friend, the Goonies.
18
00:01:01,519 --> 00:01:07,460
So what we've done here is assigned this string Goonies to the variable movies.
19
00:01:07,460 --> 00:01:12,020
And then when we reference movies at any time, then it just holds onto that string.
20
00:01:12,020 --> 00:01:16,340
So we use the string literal here to create that string object.
21
00:01:16,340 --> 00:01:19,720
Now when we assign the variable, it just springs into existence.
22
00:01:19,720 --> 00:01:22,039
There's no need to declare any types in Ruby.
23
00:01:22,039 --> 00:01:23,740
Ruby is a dynamically typed language.
24
00:01:23,740 --> 00:01:28,640
So right now, the movie's type, if you will, is the string object.
25
00:01:28,640 --> 00:01:32,919
So we've seen that we can use variables inside of other strings.
26
00:01:32,919 --> 00:01:38,179
So we can say Mikey's favorite movie is, and we're inside of a double quoted string here,
27
00:01:38,179 --> 00:01:42,179
so I can use the interpolation syntax.
28
00:01:42,179 --> 00:01:45,500
And we've got Mikey's favorite movie as Goonies again.
29
00:01:45,500 --> 00:01:47,300
We can also reassign to variables.
30
00:01:47,300 --> 00:01:53,780
So now let's say I change my mind, my favorite movie is now Ghostbusters, right?
31
00:01:53,780 --> 00:01:57,420
So I've changed the variable and now points to a different string object.
32
00:01:57,420 --> 00:02:01,780
And if I rerun this double quoted string, well, now I have Mikey's favorite movie is
33
00:02:01,780 --> 00:02:06,340
Ghostbusters because that movie variable holds onto a string object.
34
00:02:06,340 --> 00:02:11,579
In fact, if we look at the class of movie, we can see that it was created from the string
35
00:02:11,579 --> 00:02:12,740
class.
36
00:02:12,740 --> 00:02:15,040
We've also seen that variables can point to numbers.
37
00:02:15,040 --> 00:02:17,000
So let's try a few more of those.
38
00:02:17,000 --> 00:02:19,740
Let's say we're doing some ranking system.
39
00:02:19,740 --> 00:02:22,260
So we have a number of thumbs ups.
40
00:02:22,260 --> 00:02:25,900
Let's say we've got ten thumbs ups, okay?
41
00:02:25,900 --> 00:02:30,700
And then we've got maybe, oh, I don't know, two thumbs downs, right?
42
00:02:30,700 --> 00:02:38,060
So the overall rank could be, we've seen part of this before, thumbs up minus thumbs down,
43
00:02:38,060 --> 00:02:39,280
just like that.
44
00:02:39,280 --> 00:02:40,780
So now we've got a rank of eight.
45
00:02:40,780 --> 00:02:45,220
And if we look at the class of rank, it is a fixed num.
46
00:02:45,220 --> 00:02:48,420
So we're dealing with a fixed num object here.
47
00:02:48,420 --> 00:02:51,300
We also saw that we could create times in Ruby.
48
00:02:51,300 --> 00:02:55,420
In this case, I'm gonna assign it to the variable current time.
49
00:02:55,420 --> 00:02:58,400
It's gonna hold onto a new time object.
50
00:02:58,400 --> 00:03:01,240
And then we could use that current time variable inside of another string.
51
00:03:01,240 --> 00:03:08,500
We could say like the time is, and then substitute that in, current time, just like that.
52
00:03:08,500 --> 00:03:13,240
So we have a variable, oh, and in this case, if you look at current time's class, well,
53
00:03:13,240 --> 00:03:14,240
it's a time object.
54
00:03:14,240 --> 00:03:18,940
So we've seen how we can use variables to store strings, we can use variables to store
55
00:03:18,940 --> 00:03:21,700
numbers, and we can use variables to store any object type.
56
00:03:21,700 --> 00:03:24,060
In the last case, we used a time object.
57
00:03:24,060 --> 00:03:29,260
Now, it's important to remember that variables hold references to objects, not the objects
58
00:03:29,260 --> 00:03:30,260
themselves.
59
00:03:30,260 --> 00:03:31,260
Right.
60
00:03:31,260 --> 00:03:34,500
You could think of a variable like a pointer to an object, and the object just lives somewhere
61
00:03:34,500 --> 00:03:35,500
in memory.
62
00:03:35,500 --> 00:03:36,500
Right.
63
00:03:36,500 --> 00:03:37,500
Well, let's look at an example of that.
64
00:03:37,500 --> 00:03:38,500
Yeah, let's look at an example.
65
00:03:38,500 --> 00:03:42,140
Okay, back in IRB, I'm just gonna clean up the screen a little bit here.
66
00:03:42,140 --> 00:03:43,620
We're still in an IRB session.
67
00:03:43,620 --> 00:03:45,380
I'm gonna create a new variable here.
68
00:03:45,380 --> 00:03:52,460
I'm gonna call it my favorite movie, and yes, I'm gonna assign Goonies to that variable.
69
00:03:52,460 --> 00:03:54,060
So we've got my favorite movie.
70
00:03:54,060 --> 00:03:55,820
Now I'm gonna create your favorite movie.
71
00:03:55,820 --> 00:03:57,300
That's a little presumptuous.
72
00:03:57,300 --> 00:04:02,460
Yeah, but I know you like Goonies too, so I'm gonna assign my favorite movie to your
73
00:04:02,460 --> 00:04:04,400
favorite movie.
74
00:04:04,400 --> 00:04:07,100
So we've basically aliased one variable with another.
75
00:04:07,100 --> 00:04:10,940
Your favorite movie points to my favorite movie, which is Goonies.
76
00:04:10,940 --> 00:04:11,940
So let's have a look.
77
00:04:11,940 --> 00:04:17,459
So if I look at my favorite movie, we can call the method object ID.
78
00:04:17,459 --> 00:04:19,339
That variable points to that object.
79
00:04:19,339 --> 00:04:21,339
It's just the number of the object in memory.
80
00:04:21,339 --> 00:04:27,040
If we look at your favorite movie's object ID, well, it's the same object.
81
00:04:27,040 --> 00:04:29,760
And that's because we've got two variables here.
82
00:04:29,760 --> 00:04:31,780
They both point to the same object.
83
00:04:31,780 --> 00:04:34,920
It's this string Goonies that lives in memory somewhere.
84
00:04:34,920 --> 00:04:38,820
So just to show that a little bit different way, what if I have my favorite movie and
85
00:04:38,820 --> 00:04:40,060
I change the first character?
86
00:04:40,060 --> 00:04:43,900
And I can do that with a string just by indexing to the zeroth character there.
87
00:04:43,900 --> 00:04:47,580
And I'm gonna change that first character to an L.
88
00:04:47,580 --> 00:04:52,620
Now if I print out my favorite movie, it's Looney's, which is actually a movie name.
89
00:04:52,620 --> 00:04:57,060
And if I print out your favorite movie, oh, it's Looney's as well, because both of those
90
00:04:57,060 --> 00:04:59,340
variables point to the same object.
91
00:04:59,340 --> 00:05:02,740
I changed that object to the Looney's string.
92
00:05:02,740 --> 00:05:06,940
So both your favorite movie and my favorite movie is now Looney's.
93
00:05:06,940 --> 00:05:08,900
I'm tired of being tied to you.
94
00:05:08,900 --> 00:05:11,219
I want my favorite movie to be Ghostbusters.
95
00:05:11,219 --> 00:05:12,820
Well, we can do that.
96
00:05:12,820 --> 00:05:16,140
I'll change my favorite movie to be Ghostbusters.
97
00:05:16,140 --> 00:05:20,539
Okay, now my favorite movie is Ghostbusters.
98
00:05:20,539 --> 00:05:23,700
Your favorite movie is still Looney's, however.
99
00:05:23,700 --> 00:05:28,900
So now we have two variables, my favorite movie, your favorite movie, and two objects,
100
00:05:28,900 --> 00:05:30,539
two string objects.
101
00:05:30,539 --> 00:05:33,140
So let's see that in slow motion.
102
00:05:33,140 --> 00:05:36,620
My favorite movie references a string object, Goonies.
103
00:05:36,620 --> 00:05:41,780
When we assign my favorite movie to your favorite movie, your favorite movie now references
104
00:05:41,780 --> 00:05:44,820
the same string object, Goonies.
105
00:05:44,820 --> 00:05:47,620
Note that a new string object wasn't created here.
106
00:05:47,620 --> 00:05:51,100
We have one string object and two variables.
107
00:05:51,100 --> 00:05:56,880
Now when we change the first character of my favorite movie to L, both variables get
108
00:05:56,880 --> 00:05:58,580
changed to Looney's.
109
00:05:58,580 --> 00:06:04,500
Finally, we say we assign my favorite movie to a new string object, Ghostbusters.
110
00:06:04,500 --> 00:06:09,660
The variable my favorite movie now references a different string object, but your favorite
111
00:06:09,660 --> 00:06:11,980
movie still points to Looney's.
112
00:06:11,980 --> 00:06:15,940
So now we have two string objects and two variables.
113
00:06:15,940 --> 00:06:19,860
So now we know that when we use a variable, we're really using an object.
114
00:06:19,860 --> 00:06:24,740
Yeah, and on one hand, a string object is nothing more than a sequence of characters,
115
00:06:24,740 --> 00:06:26,860
but objects can also do things.
116
00:06:26,860 --> 00:06:30,980
And we tell an object to do something by calling a method on that object.
117
00:06:30,980 --> 00:06:34,700
So sometimes you hear this referred to as sending the object a message.
118
00:06:34,700 --> 00:06:36,540
So let's give that a whirl.
119
00:06:36,540 --> 00:06:38,380
So to call a method, we need an object.
120
00:06:38,380 --> 00:06:41,740
We're going to go back to our favorite movie object here.
121
00:06:41,740 --> 00:06:45,300
And then we can just call a method by using a dot syntax.
122
00:06:45,300 --> 00:06:47,380
We take the object and then we call the method.
123
00:06:47,380 --> 00:06:52,620
In this case, we want to get the length of that string and it's seven characters long.
124
00:06:52,620 --> 00:06:54,180
So maybe we want to reverse the string.
125
00:06:54,180 --> 00:06:58,420
Well, we can call the reverse method on that object and that's just going to return a new
126
00:06:58,420 --> 00:07:01,660
string object in its reverse sequence here.
127
00:07:01,660 --> 00:07:02,660
So Goonies reversed.
128
00:07:02,660 --> 00:07:03,660
Synog.
129
00:07:03,660 --> 00:07:04,660
Yeah.
130
00:07:04,660 --> 00:07:07,840
Does it sound like the next Academy Award winning film?
131
00:07:07,840 --> 00:07:12,540
You never know what an Academy Award winning film is going to be named, right?
132
00:07:12,540 --> 00:07:17,980
So that's a method that doesn't take any parameters, but methods can also take parameters.
133
00:07:17,980 --> 00:07:21,900
So let's look at a method like center, which is going to center a string between a certain
134
00:07:21,900 --> 00:07:23,180
number of characters.
135
00:07:23,180 --> 00:07:26,040
And we pass in that number of characters as a parameter.
136
00:07:26,040 --> 00:07:27,720
They go between parentheses.
137
00:07:27,720 --> 00:07:29,100
So this is going to center a movie.
138
00:07:29,100 --> 00:07:30,940
Actually, I want it about 10 characters.
139
00:07:30,940 --> 00:07:35,580
So it centers Goonies within a 10 character string.
140
00:07:35,580 --> 00:07:38,380
So that's one parameter being passed to a method.
141
00:07:38,380 --> 00:07:40,580
There are also methods that take multiple parameters.
142
00:07:40,580 --> 00:07:43,180
One of those methods is the ljust method.
143
00:07:43,180 --> 00:07:47,900
And ljust takes the first parameter is how long we want the string to be, let's say 30
144
00:07:47,900 --> 00:07:52,860
characters, and then what the character should be used for padding that.
145
00:07:52,860 --> 00:07:58,060
So we just separate the two parameters by a comma there, and we get back a left justified
146
00:07:58,060 --> 00:08:01,420
string padded with dots in this case.
147
00:08:01,420 --> 00:08:05,740
Now numbers are objects too, so we can call methods on numbers in Ruby.
148
00:08:05,740 --> 00:08:08,540
So here's a number, rank equals eight.
149
00:08:08,540 --> 00:08:12,420
And then we can call, we've already seen we can call the 2s method that converts it to
150
00:08:12,420 --> 00:08:13,460
a string.
151
00:08:13,460 --> 00:08:18,540
We could call something like the 2f method that converts it to a float, or we might,
152
00:08:18,540 --> 00:08:22,940
if we wanted to convert it to an integer, which it already is, but there's also a 2i
153
00:08:22,940 --> 00:08:23,940
method on those.
154
00:08:23,940 --> 00:08:27,140
So we've got our fixnum eight there.
155
00:08:27,140 --> 00:08:31,220
Now you may be used to doing multiplication or any sort of arithmetic on numbers.
156
00:08:31,220 --> 00:08:35,039
Say we take the rank and we multiply it by two.
157
00:08:35,039 --> 00:08:36,980
And that looks to be like something special.
158
00:08:36,980 --> 00:08:38,920
It doesn't look like a method call.
159
00:08:38,920 --> 00:08:43,560
But in reality, what's happening underneath is Ruby is actually calling a method called
160
00:08:43,560 --> 00:08:45,000
times.
161
00:08:45,000 --> 00:08:49,740
It's actually the operator, the times operator, and then it's passing in the parameter of
162
00:08:49,740 --> 00:08:50,800
two.
163
00:08:50,800 --> 00:08:51,940
So we get 16.
164
00:08:51,940 --> 00:08:57,900
So this form here is really just a shortcut for calling the method.
165
00:08:57,900 --> 00:09:00,260
We've also seen how to create time objects.
166
00:09:00,260 --> 00:09:04,340
So let me create another time object here, time.new.
167
00:09:04,340 --> 00:09:06,380
And time objects have methods as well.
168
00:09:06,380 --> 00:09:10,500
For example, we can take our current time and ask it what is the current hour.
169
00:09:10,500 --> 00:09:14,360
It's the 16th hour and the current minute.
170
00:09:14,360 --> 00:09:17,060
So let's recap what's going on here.
171
00:09:17,060 --> 00:09:20,820
Suppose we have a variable called movie that points to a string object.
172
00:09:20,820 --> 00:09:24,260
If we want to reverse it, we can call the reverse method.
173
00:09:24,260 --> 00:09:26,900
The reverse method doesn't take any parameters.
174
00:09:26,900 --> 00:09:30,160
It returns a new string in reverse order.
175
00:09:30,160 --> 00:09:35,120
String objects also have a method called center, which does take one parameter.
176
00:09:35,120 --> 00:09:39,140
Parameters come after the method name and are surrounded by parentheses.
177
00:09:39,140 --> 00:09:41,340
Methods can also take multiple parameters.
178
00:09:41,340 --> 00:09:45,300
For example, the ljust method we just saw takes two parameters.
179
00:09:45,300 --> 00:09:50,500
And when you pass in multiple parameters, you separate them with a comma.
180
00:09:50,500 --> 00:09:51,500
Here's the takeaway.
181
00:09:51,500 --> 00:09:55,060
You always call methods on an object using the dot notation.
182
00:09:55,060 --> 00:10:00,340
The object, sometimes called the receiver of the message, is always on the left.
183
00:10:00,340 --> 00:10:06,220
And the method name and parameters, or the message being sent, is always on the right.
184
00:10:06,220 --> 00:10:10,220
Now you might be asking yourself, how would I know that the center method takes one parameter
185
00:10:10,220 --> 00:10:12,140
and the ljust method takes two parameters?
186
00:10:12,140 --> 00:10:13,140
Right.
187
00:10:13,140 --> 00:10:16,420
Or let's say what if I want to capitalize a movie, or what if I just want to know what
188
00:10:16,420 --> 00:10:17,980
else I can do with a string object?
189
00:10:17,980 --> 00:10:20,940
Yeah, the string object has a boatload of methods on it.
190
00:10:20,940 --> 00:10:25,700
And if you look at the Ruby standard library as a whole, I think there's over 9,000 methods.
191
00:10:25,700 --> 00:10:27,300
We need to teach them how to fish.
192
00:10:27,300 --> 00:10:29,180
It's time to go fishing.
193
00:10:29,180 --> 00:10:30,700
So I'm over at a terminal here.
194
00:10:30,700 --> 00:10:32,440
I've got out of IRB.
195
00:10:32,440 --> 00:10:34,460
And the command I want to show you is ri.
196
00:10:34,460 --> 00:10:35,460
Now this comes with Ruby.
197
00:10:35,460 --> 00:10:37,860
When you install Ruby, you get the ri command.
198
00:10:37,860 --> 00:10:41,780
And it's just a command line utility for viewing documentation.
199
00:10:41,780 --> 00:10:45,020
So let's say I wanted to find all the documentation for the string class.
200
00:10:45,020 --> 00:10:47,120
I can just say ri string.
201
00:10:47,120 --> 00:10:50,260
And it gives me a little explanation of what a string does.
202
00:10:50,260 --> 00:10:53,080
And if I hit the space bar, I can sort of scroll through this.
203
00:10:53,080 --> 00:10:57,660
You see here all the instance methods, methods that I can call on string objects.
204
00:10:57,660 --> 00:10:59,140
And we've used some of those already in here.
205
00:10:59,140 --> 00:11:00,820
We got the length, for example.
206
00:11:00,820 --> 00:11:02,100
We reversed the string.
207
00:11:02,100 --> 00:11:04,060
I can keep scrolling back down.
208
00:11:04,060 --> 00:11:05,640
And then we get out of ri.
209
00:11:05,640 --> 00:11:09,420
So if you know of a particular method you want to find out about, you can use ri, give
210
00:11:09,420 --> 00:11:10,939
the class name string.
211
00:11:10,939 --> 00:11:14,560
And let's say we're looking for some information about how to center a string.
212
00:11:14,560 --> 00:11:16,740
Just type in the method name center.
213
00:11:16,740 --> 00:11:19,980
And then it pulls up just the documentation for the center method.
214
00:11:19,980 --> 00:11:20,980
And it's kind of handy.
215
00:11:20,980 --> 00:11:23,580
It shows you you have your string object you call center.
216
00:11:23,580 --> 00:11:25,540
It takes two parameters here.
217
00:11:25,540 --> 00:11:27,819
The second parameter is optional in this case.
218
00:11:27,819 --> 00:11:30,700
And returns a new string object.
219
00:11:30,700 --> 00:11:34,860
But what I really like about the ri documentation are these little examples down here.
220
00:11:34,860 --> 00:11:37,060
Because they're executable examples.
221
00:11:37,060 --> 00:11:39,300
You can actually copy one of these out.
222
00:11:39,300 --> 00:11:40,940
I'm just going to copy that.
223
00:11:40,940 --> 00:11:45,940
I'm going to get out of that ri session, go into an IRB session where we can run some
224
00:11:45,940 --> 00:11:46,940
code.
225
00:11:46,940 --> 00:11:49,060
I can just paste in that example.
226
00:11:49,060 --> 00:11:52,060
And sure enough, it's running code so I can see how center works.
227
00:11:52,060 --> 00:11:55,160
Yeah, that is so helpful for experimenting.
228
00:11:55,160 --> 00:11:56,380
Just trying things out.
229
00:11:56,380 --> 00:12:00,500
Looking in the documentation, jumping out to IRB, and seeing what it does and playing
230
00:12:00,500 --> 00:12:01,500
with it.
231
00:12:01,500 --> 00:12:03,140
Yeah, and we can just change the example here.
232
00:12:03,140 --> 00:12:06,100
You might want to see, okay, well, what happens if I use an asterisk?
233
00:12:06,100 --> 00:12:07,340
Sure enough, it pads it that way.
234
00:12:07,340 --> 00:12:11,740
So it's a really good way, as you said, to get feedback about how methods work.
235
00:12:11,740 --> 00:12:13,740
And then we can get back out of IRB.
236
00:12:13,740 --> 00:12:17,620
And then let's say we wanted to look at other center methods.
237
00:12:17,620 --> 00:12:23,300
So if I type in just center, the method name, well, it's only defined in one class string.
238
00:12:23,300 --> 00:12:26,860
So I'm going to get the documentation for the center method on string.
239
00:12:26,860 --> 00:12:31,819
But if I were to try something like reverse, which is another method we use.
240
00:12:31,820 --> 00:12:35,300
Well, reverse is defined on multiple classes in Ruby.
241
00:12:35,300 --> 00:12:36,620
It's defined in array.
242
00:12:36,620 --> 00:12:39,100
It's defined in this class called ip adder.
243
00:12:39,100 --> 00:12:41,420
And then finally, it's defined in string.
244
00:12:41,420 --> 00:12:44,780
So you can either go for the whole class and then find the method you want.
245
00:12:44,780 --> 00:12:47,500
You can type in a class and a specific method.
246
00:12:47,500 --> 00:12:50,420
Or you could just try a generic method and see where it's defined.
247
00:12:50,420 --> 00:12:52,340
All right, so this is great.
248
00:12:52,340 --> 00:12:57,340
Now we know how to find out what an object can do and how to call those methods.
249
00:12:57,340 --> 00:13:00,940
But you may have noticed if you were really paying attention, some of those methods had
250
00:13:00,940 --> 00:13:03,860
a question mark or an exclamation point.
251
00:13:03,860 --> 00:13:05,060
We should take a look at those.
252
00:13:05,060 --> 00:13:07,180
Yeah, let's look at a few of those.
253
00:13:07,180 --> 00:13:11,160
So one of those methods was the empty method on the string class.
254
00:13:11,160 --> 00:13:14,740
And the name of the method was empty with a question mark at the end.
255
00:13:14,740 --> 00:13:19,300
And if we look at that, we see that we have a string we call empty question mark and it
256
00:13:19,300 --> 00:13:21,180
returns true or false.
257
00:13:21,180 --> 00:13:25,160
So this is purely convention, but methods that end in a question mark are often called
258
00:13:25,160 --> 00:13:28,980
predicate methods because they return a true or a false value.
259
00:13:28,980 --> 00:13:31,300
You can almost think of them as like asking a question.
260
00:13:31,300 --> 00:13:34,140
We're asking the string, are you empty?
261
00:13:34,140 --> 00:13:36,200
So let's try this in IRB.
262
00:13:36,200 --> 00:13:40,460
We'll create a variable with an empty string that's just double quotes.
263
00:13:40,460 --> 00:13:44,020
We'll call movie.empty question mark and we get back true.
264
00:13:44,020 --> 00:13:49,760
If we were to change movie to our favorite and we call empty again, it returns false.
265
00:13:49,760 --> 00:13:52,880
So empty question mark returns true or false.
266
00:13:52,880 --> 00:13:55,380
Let's try a couple more of those.
267
00:13:55,380 --> 00:14:01,860
We also have a method called start with and the question mark is part of the method name
268
00:14:01,860 --> 00:14:03,500
and then we can give it a character.
269
00:14:03,500 --> 00:14:05,620
So does Goonies start with G?
270
00:14:05,620 --> 00:14:06,620
Yes it does.
271
00:14:06,620 --> 00:14:09,900
We can also call something like include question mark.
272
00:14:09,900 --> 00:14:12,820
Does Goonies include the character S?
273
00:14:12,820 --> 00:14:13,920
No it doesn't.
274
00:14:13,920 --> 00:14:16,260
So that's all there is to predicate methods.
275
00:14:16,260 --> 00:14:19,740
Methods that have a question mark at the end, it's part of the method name and then just
276
00:14:19,740 --> 00:14:21,440
return true or false.
277
00:14:21,440 --> 00:14:24,540
Now how about those methods that end with an exclamation point?
278
00:14:24,540 --> 00:14:27,260
Yeah, so let me get it out of IRB.
279
00:14:27,260 --> 00:14:31,140
One of those methods that we saw, well one method we've used is the reverse method on
280
00:14:31,140 --> 00:14:38,020
strings and we saw that reverse, call it on a string object and it returns a new string
281
00:14:38,020 --> 00:14:39,020
here.
282
00:14:39,020 --> 00:14:41,939
In fact the documentation says returns a new string with the characters from string in
283
00:14:41,939 --> 00:14:43,800
reverse order.
284
00:14:43,800 --> 00:14:48,780
But there's another method, string reverse exclamation point.
285
00:14:48,780 --> 00:14:51,420
The exclamation point is part of the method name.
286
00:14:51,420 --> 00:14:54,860
If we look at the documentation for it, you notice that we're calling it on the string
287
00:14:54,860 --> 00:14:58,459
object str, but it's also returning the string object.
288
00:14:58,459 --> 00:15:00,579
So it's not creating a new string.
289
00:15:00,579 --> 00:15:04,959
And in fact the documentation says reverses the string in place.
290
00:15:04,959 --> 00:15:06,459
So let's look at that in IRB.
291
00:15:06,459 --> 00:15:10,380
In fact I'm going to go to the top of the screen here.
292
00:15:10,380 --> 00:15:15,579
I'm going to have a movie, say this is Ghostbusters.
293
00:15:15,579 --> 00:15:19,620
We've seen we can call movie.reverse that reverses the string.
294
00:15:19,620 --> 00:15:23,180
And if we look at the original object movie here, it hasn't changed.
295
00:15:23,180 --> 00:15:24,920
It's still Ghostbusters.
296
00:15:24,920 --> 00:15:29,900
But what happens if we call reverse with an exclamation point at the end?
297
00:15:29,900 --> 00:15:32,540
We get the name of a German movie.
298
00:15:32,540 --> 00:15:35,660
Another Academy Award winning movie.
299
00:15:35,660 --> 00:15:39,140
So it's reversed the string, but what happens if we look at the original object now?
300
00:15:39,140 --> 00:15:41,180
Well, it's been reversed in place.
301
00:15:41,180 --> 00:15:44,220
Those original characters were also reversed.
302
00:15:44,220 --> 00:15:48,100
So these methods that end in an exclamation point, when you see those, just think that
303
00:15:48,100 --> 00:15:52,020
they might do something slightly unexpected or slightly dangerous.
304
00:15:52,020 --> 00:15:57,260
In this case, by calling reverse with an exclamation point, we reverse the original string.
305
00:15:57,260 --> 00:16:00,820
So it's just something to be aware of when you use those methods.
306
00:16:00,820 --> 00:16:03,180
So I think we're now ready to put this all together.
307
00:16:03,180 --> 00:16:07,860
If you'll recall, our initial objective was to print out a movie listing, something like
308
00:16:07,860 --> 00:16:09,260
this.
309
00:16:09,260 --> 00:16:12,100
You know, Ruby is really good at string formatting chores like this.
310
00:16:12,100 --> 00:16:16,900
And by chaining a few string methods together, the code for this ends up being quite elegant.
311
00:16:16,900 --> 00:16:18,500
So here's what we need to do.
312
00:16:18,500 --> 00:16:22,840
You capitalize the movie title, left justify it, and include the rank.
313
00:16:22,840 --> 00:16:24,860
So let's do this with Ghostbusters.
314
00:16:24,860 --> 00:16:26,060
Okay.
315
00:16:26,060 --> 00:16:28,860
So back over in IRB, let's set ourselves up a movie.
316
00:16:28,860 --> 00:16:32,540
And I'm going to use the lowercase form of Ghostbusters here because part of our objective
317
00:16:32,540 --> 00:16:34,740
is to actually capitalize it.
318
00:16:34,740 --> 00:16:37,140
And I want to assign the capitalize movie to a variable.
319
00:16:37,140 --> 00:16:38,140
I'm going to call it title.
320
00:16:38,140 --> 00:16:41,500
I'm going to call movie.capitalize.
321
00:16:41,500 --> 00:16:43,939
That's one of the methods that's on the string class.
322
00:16:43,940 --> 00:16:46,960
And we have back Ghostbusters in the capitalize form.
323
00:16:46,960 --> 00:16:48,180
So we have our first thing done.
324
00:16:48,180 --> 00:16:49,580
Yeah, we've got the first thing done.
325
00:16:49,580 --> 00:16:51,660
The second thing is just to left justify it, right?
326
00:16:51,660 --> 00:16:53,860
So we know how to do an L just.
327
00:16:53,860 --> 00:16:57,500
We'll do it 30 padded with dots, just like that.
328
00:16:57,500 --> 00:16:59,100
So we got the second thing done.
329
00:16:59,100 --> 00:17:00,900
We're so far so good, yeah?
330
00:17:00,900 --> 00:17:01,900
Right.
331
00:17:01,900 --> 00:17:03,220
So we did this in two separate steps.
332
00:17:03,220 --> 00:17:07,100
We first capitalized the movie, assigned it the title, and then we left justified that
333
00:17:07,100 --> 00:17:08,100
title.
334
00:17:08,100 --> 00:17:10,780
We can do this in one fell swoop if we want to.
335
00:17:10,780 --> 00:17:15,579
We can set our title variable, take movie, call capitalize.
336
00:17:15,579 --> 00:17:17,619
Capitalize returns a string.
337
00:17:17,619 --> 00:17:20,700
So we just have a string object right here at this point in the code.
338
00:17:20,700 --> 00:17:26,420
So we can just turn around and call left justify or L just on it and left justified that way.
339
00:17:26,420 --> 00:17:30,240
So we're chaining together the capitalize and the L just methods.
340
00:17:30,240 --> 00:17:33,420
And we have our title all formatted the way we want.
341
00:17:33,420 --> 00:17:34,840
So now we just need the rank.
342
00:17:34,840 --> 00:17:35,940
We'll have a rank variable.
343
00:17:35,940 --> 00:17:38,160
We'll set it to nine.
344
00:17:38,160 --> 00:17:40,139
And then we know how to put these two things together.
345
00:17:40,140 --> 00:17:45,100
We can concatenate them as a single quoted string or we can use a double quoted string,
346
00:17:45,100 --> 00:17:46,220
which I'll do here.
347
00:17:46,220 --> 00:17:52,840
And we'll just have our title and then a space and then our rank in the double quoted string.
348
00:17:52,840 --> 00:17:56,900
And now we've got our formatted movie listing.
349
00:17:56,900 --> 00:17:58,260
Give yourself a pat on the back.
350
00:17:58,260 --> 00:18:00,400
You learned a lot in this section.
351
00:18:00,400 --> 00:18:02,740
You learned about variables and assignment.
352
00:18:02,740 --> 00:18:06,020
You learned about how to call methods both with and without parameters.
353
00:18:06,020 --> 00:18:09,500
You learned how to access the docs and predicate methods.
354
00:18:09,500 --> 00:18:11,740
You even learned how to chain methods together.
355
00:18:11,740 --> 00:18:15,860
So now you know everything you need to know to format how your players are printed when
356
00:18:15,860 --> 00:18:17,180
you start the game.
357
00:18:17,180 --> 00:18:19,980
And now you can give it a little style.
358
00:18:19,980 --> 00:18:21,260
So give that a go in the exercise.
359
00:18:21,260 --> 00:18:25,460
And when we come back, we'll clear up some of the mystery around the put as method.
360
00:18:25,460 --> 00:18:26,460
We'll see you then.
361
00:18:26,460 --> 00:18:27,460
My favorite movie is your favorite.
362
00:18:27,460 --> 00:18:28,460
Wait a second.
363
00:18:28,460 --> 00:18:43,460
My favorite movie is your favorite.
32018
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.