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:06,640
Hey Mike, we've received a few feature requests for our game.
2
00:00:06,640 --> 00:00:07,640
Oh yeah?
3
00:00:07,640 --> 00:00:08,640
Yeah.
4
00:00:08,640 --> 00:00:09,640
Get me.
5
00:00:09,640 --> 00:00:10,640
Alright, so Mo sent an email.
6
00:00:10,640 --> 00:00:15,120
He said, if I want to vary the number of rounds to the game, I have to change the Ruby code.
7
00:00:15,120 --> 00:00:18,320
And I'd like to be able to specify the number of rounds from the command line.
8
00:00:18,320 --> 00:00:19,840
Oh, that sounds like a good idea.
9
00:00:19,840 --> 00:00:21,800
Yeah, yeah, okay.
10
00:00:21,800 --> 00:00:23,680
Now Larry's cousin tweets.
11
00:00:23,680 --> 00:00:26,200
Wait, Larry's cousin's on Twitter?
12
00:00:26,200 --> 00:00:27,200
Okay.
13
00:00:27,200 --> 00:00:28,200
Okay.
14
00:00:28,200 --> 00:00:33,080
Alright, Larry's cousin tweets, I would like to play the game with different players.
15
00:00:33,080 --> 00:00:35,280
Does he not get along with Mo and Curly?
16
00:00:35,280 --> 00:00:36,280
Yeah, I don't know.
17
00:00:36,280 --> 00:00:37,280
Okay, alright.
18
00:00:37,280 --> 00:00:38,960
But I don't know a smidgen of Ruby.
19
00:00:38,960 --> 00:00:42,860
Well, unfortunately he hasn't taken this fine Ruby course, but you know, we could probably
20
00:00:42,860 --> 00:00:46,239
put all the players in a file and then read from that file.
21
00:00:46,239 --> 00:00:47,239
Okay, alright.
22
00:00:47,239 --> 00:00:50,360
Alright, and then Curly writes us.
23
00:00:50,360 --> 00:00:55,600
Curly says, I want to be able to save off the high scores and email them to my buddies.
24
00:00:55,600 --> 00:00:57,360
But only when he wins, of course.
25
00:00:57,360 --> 00:00:58,360
Oh, of course.
26
00:00:58,360 --> 00:01:01,760
Well, I mean, these are all typical requests you would get for any Ruby program.
27
00:01:01,760 --> 00:01:04,580
In fact, our movie app could use some of these requests.
28
00:01:04,580 --> 00:01:07,840
So let's just start with the command line input part of it.
29
00:01:07,840 --> 00:01:11,560
So back in our flix.rb file here of our movie app, what we'd really like to do is instead
30
00:01:11,560 --> 00:01:16,560
of hard coding this number of plays to a three, we want to take this as command line input.
31
00:01:16,560 --> 00:01:20,360
So I'm just going to copy or comment these out just for a second here.
32
00:01:20,360 --> 00:01:25,980
And we want to prompt, we're going to say, how many viewings?
33
00:01:25,980 --> 00:01:26,980
Like that.
34
00:01:26,980 --> 00:01:29,440
And we want to get some input from the user.
35
00:01:29,440 --> 00:01:33,060
So we've seen that put s will print a string out to the console.
36
00:01:33,060 --> 00:01:39,780
To get input from the console, we can just sign a variable and we call the get s method.
37
00:01:39,780 --> 00:01:43,720
And this is going to get the next line from standard input or it'll return nil when the
38
00:01:43,720 --> 00:01:45,600
end of file is reached.
39
00:01:45,600 --> 00:01:53,400
And then I'll just print out down here, enjoy your answer viewings, dot dot dot.
40
00:01:53,400 --> 00:01:54,400
Okay.
41
00:01:54,400 --> 00:01:57,440
So to run this, I'm not going to run it inside of TextMate because we've actually got to
42
00:01:57,440 --> 00:01:58,440
type in a number here.
43
00:01:58,440 --> 00:02:03,760
So I'm going to run it over here on the terminal, rubyflix.rb.
44
00:02:03,760 --> 00:02:05,120
It says how many viewings?
45
00:02:05,120 --> 00:02:06,120
Let's say we want to do three.
46
00:02:06,120 --> 00:02:10,600
And it just says enjoy your, and there's a line break there, viewings.
47
00:02:10,600 --> 00:02:12,740
So that's not quite right.
48
00:02:12,740 --> 00:02:15,520
We want to take that line break off at the end.
49
00:02:15,520 --> 00:02:19,720
And get s returns a string here and we can call the chomp method.
50
00:02:19,720 --> 00:02:24,440
And chomp is just going to remove any return or enter characters hanging out at the end
51
00:02:24,440 --> 00:02:25,840
of that string.
52
00:02:25,840 --> 00:02:27,760
So now if we run it, how many viewings?
53
00:02:27,760 --> 00:02:28,760
Three.
54
00:02:28,760 --> 00:02:30,400
Enjoy your three viewings all in the same line.
55
00:02:30,400 --> 00:02:31,400
Perfect.
56
00:02:31,400 --> 00:02:35,280
So now we want to pass this answer, the number of viewings, over to our play method.
57
00:02:35,280 --> 00:02:36,880
So let's just move this code down here.
58
00:02:36,880 --> 00:02:39,880
So we're prompting.
59
00:02:39,880 --> 00:02:43,200
When we call play, we want to take answer.
60
00:02:43,200 --> 00:02:47,440
Now because we took this from standard input, answer is going to be a string and the play
61
00:02:47,440 --> 00:02:48,720
method expects an integer.
62
00:02:48,720 --> 00:02:53,040
So let's just convert it to an integer like that, calling 2i.
63
00:02:53,040 --> 00:02:55,600
Now back over in the command line, if we run it, how many viewings?
64
00:02:55,600 --> 00:02:57,240
We want three.
65
00:02:57,240 --> 00:02:58,240
There we go.
66
00:02:58,240 --> 00:03:01,680
We got three viewings of all of our movies.
67
00:03:01,680 --> 00:03:05,020
It would be nice if we could enter a number, say three, it would go through all those viewings
68
00:03:05,020 --> 00:03:09,640
and then it would just prompt us again for how many more viewings we wanted to do.
69
00:03:09,640 --> 00:03:14,079
So to do that, down in here, we can actually add just a real primitive looping construct.
70
00:03:14,080 --> 00:03:18,920
The loop in Ruby just takes a block, do end, like that.
71
00:03:18,920 --> 00:03:20,600
I'm going to say how many viewings.
72
00:03:20,600 --> 00:03:26,520
I'm going to put a new line here just so that we see each different prompt that comes up.
73
00:03:26,520 --> 00:03:29,720
And actually I need this play inside of there as well because we want to play it a number
74
00:03:29,720 --> 00:03:30,720
of times.
75
00:03:30,720 --> 00:03:31,720
Okay.
76
00:03:31,720 --> 00:03:36,640
So we're just going to loop around asking how many viewings and then call play.
77
00:03:36,640 --> 00:03:38,360
So let's try that.
78
00:03:38,360 --> 00:03:39,360
How many viewings?
79
00:03:39,360 --> 00:03:40,700
Let's say we want one.
80
00:03:40,700 --> 00:03:41,700
We got one viewing.
81
00:03:41,700 --> 00:03:43,080
Then we're prompted at the bottom again.
82
00:03:43,080 --> 00:03:44,760
How many more viewings?
83
00:03:44,760 --> 00:03:46,640
Let's say three, maybe 10.
84
00:03:46,640 --> 00:03:48,200
There's only one problem.
85
00:03:48,200 --> 00:03:49,680
We can't get out of here.
86
00:03:49,680 --> 00:03:51,760
We just have to keep entering numbers.
87
00:03:51,760 --> 00:03:53,580
It's a true movie marathon.
88
00:03:53,580 --> 00:03:55,720
This is a very long movie marathon.
89
00:03:55,720 --> 00:03:57,200
You just keep watching, right?
90
00:03:57,200 --> 00:04:01,320
Now we can get out of here by doing Control C, but that's not very good.
91
00:04:01,320 --> 00:04:05,280
So rather than just continuously looping, we'd like to arrange things here so that
92
00:04:05,280 --> 00:04:10,000
the user can type in like little commands and then we'll respond to those commands
93
00:04:10,000 --> 00:04:12,960
using a conditional and take a different path of code depending on what they want us to
94
00:04:12,960 --> 00:04:13,960
do.
95
00:04:13,960 --> 00:04:17,140
So we're going to return to our old friend the case statement to do that.
96
00:04:17,140 --> 00:04:21,480
So inside of here, inside of our looping construct, let's say we want to say how many viewings
97
00:04:21,480 --> 00:04:27,920
and then we'll also print out that they can type in say quit to exit the program.
98
00:04:27,920 --> 00:04:32,120
Now, so we want to respond to some commands and we're going to use the case.
99
00:04:32,120 --> 00:04:33,120
We've got our answer here.
100
00:04:33,120 --> 00:04:35,360
We're going to set up our case statement.
101
00:04:35,360 --> 00:04:39,380
Its target is going to be whatever's in that answer variable.
102
00:04:39,380 --> 00:04:41,400
So let's just try a really easy one here.
103
00:04:41,400 --> 00:04:43,159
We've got win.
104
00:04:43,159 --> 00:04:48,080
So we want to say if they enter something like quit or we'll also support maybe exit
105
00:04:48,080 --> 00:04:50,280
just in case they want to try that.
106
00:04:50,280 --> 00:04:54,159
Then what we'll do here is we'll take our playlist and we'll print the stats because
107
00:04:54,159 --> 00:04:55,520
we're all done.
108
00:04:55,520 --> 00:04:59,400
And then we're going to call break, which will break us out of this surrounding loop
109
00:04:59,400 --> 00:05:01,840
because the game's over.
110
00:05:01,840 --> 00:05:02,840
We do that.
111
00:05:02,840 --> 00:05:06,780
We don't need print stats down here because we've already got it inside of this win statement.
112
00:05:06,780 --> 00:05:09,460
Now we're responding these commands, quit or exit.
113
00:05:09,460 --> 00:05:13,000
What happens if they enter an uppercase Q or an uppercase E?
114
00:05:13,000 --> 00:05:17,280
Well we can solve that just by after we get the get us here and we chomp it, we actually
115
00:05:17,280 --> 00:05:18,640
call downcase.
116
00:05:18,640 --> 00:05:21,719
And that way every command that gets typed in, we're going to automatically downcase
117
00:05:21,719 --> 00:05:26,479
it and then we're going to match against these particular words.
118
00:05:26,479 --> 00:05:33,479
So if they don't enter quit or exit, then we just want the else part of this to be please
119
00:05:33,479 --> 00:05:37,080
enter a number or quit.
120
00:05:37,080 --> 00:05:41,599
So if they don't enter quit or exit, then there's this fallout case where we just tell
121
00:05:41,599 --> 00:05:42,919
them what to do.
122
00:05:42,919 --> 00:05:44,840
And then we're missing one more here.
123
00:05:44,840 --> 00:05:47,880
Now we also want to branch for if they enter a number.
124
00:05:47,880 --> 00:05:49,800
So we want a different win here.
125
00:05:49,800 --> 00:05:52,400
And I'm going to use a Ruby regular expression to do this.
126
00:05:52,400 --> 00:05:58,360
Regular expression goes between these slash characters and I'm going to say the regular
127
00:05:58,360 --> 00:06:05,360
expression or the thing that they type in has to start with one or more numbers and
128
00:06:05,360 --> 00:06:06,360
also end with that.
129
00:06:06,360 --> 00:06:09,280
So basically just like match against any number.
130
00:06:09,280 --> 00:06:12,740
Now regular expressions in Ruby is a whole course unto itself.
131
00:06:12,740 --> 00:06:15,120
This is just a little regular expression.
132
00:06:15,120 --> 00:06:18,560
It's just going to match the characters against a number.
133
00:06:18,560 --> 00:06:23,080
So we're going to get into this win clause here if they've entered a number.
134
00:06:23,080 --> 00:06:24,520
What do we want to do if they enter a number?
135
00:06:24,520 --> 00:06:31,760
Well we just want to play the playlist, passing in that number and we can drop this.
136
00:06:31,760 --> 00:06:32,760
So let's go through this again.
137
00:06:32,760 --> 00:06:34,080
We get the answer.
138
00:06:34,080 --> 00:06:36,880
If it's a number, we play that many times.
139
00:06:36,880 --> 00:06:40,640
If it's quit or exit, then we're going to print the stats and break out of this loop.
140
00:06:40,640 --> 00:06:44,479
Otherwise if it's neither a number or quit or exit, then we just have to tell them, hey,
141
00:06:44,479 --> 00:06:46,080
you got to enter a number or quit.
142
00:06:46,080 --> 00:06:47,479
So let's try that out.
143
00:06:47,479 --> 00:06:48,479
Go back to the console.
144
00:06:48,479 --> 00:06:50,240
I'm just going to clean that up a little bit.
145
00:06:50,240 --> 00:06:51,799
How many viewings?
146
00:06:51,799 --> 00:06:54,520
Let's say one, three.
147
00:06:54,520 --> 00:06:55,640
Now we're all done.
148
00:06:55,640 --> 00:06:57,520
Let's enter something that doesn't work.
149
00:06:57,520 --> 00:06:59,039
Just enter like ABC.
150
00:06:59,039 --> 00:07:01,479
It says please enter a number or quit.
151
00:07:01,480 --> 00:07:06,760
We enter quit and then it prints out all the stats for the movies that got run.
152
00:07:06,760 --> 00:07:08,160
All right, so far so good.
153
00:07:08,160 --> 00:07:12,560
Now we need to help out our non-programming friends who want to add their own movies.
154
00:07:12,560 --> 00:07:14,840
Well we could let them put the movies inside of a file.
155
00:07:14,840 --> 00:07:18,480
We just use a comma separated file with the movie name in the rank and then we read it
156
00:07:18,480 --> 00:07:19,480
in.
157
00:07:19,480 --> 00:07:20,480
Sure, sure.
158
00:07:20,480 --> 00:07:22,200
It could look like this.
159
00:07:22,200 --> 00:07:25,200
Then they can put in whatever movies and rankings they want.
160
00:07:25,200 --> 00:07:27,280
They don't need to write any Ruby code.
161
00:07:27,280 --> 00:07:33,000
Then our application will read in that file and create the movie objects for them automatically.
162
00:07:33,000 --> 00:07:34,359
So here's our movie file.
163
00:07:34,359 --> 00:07:37,320
We've got our three movies in here in a comma separated format.
164
00:07:37,320 --> 00:07:38,919
We want to be able to read these in.
165
00:07:38,919 --> 00:07:43,280
I just created a quick file called files.rb so we can play around with files a little
166
00:07:43,280 --> 00:07:44,280
bit.
167
00:07:44,280 --> 00:07:45,479
So opening files in Ruby is really easy.
168
00:07:45,479 --> 00:07:47,000
We use the file class.
169
00:07:47,000 --> 00:07:52,919
We call the open method and then we pass in the name of the file, movies.csv in this case.
170
00:07:52,919 --> 00:07:54,320
That returns a file object.
171
00:07:54,320 --> 00:07:56,559
We can assign it to a variable.
172
00:07:56,560 --> 00:08:00,040
And then we would do something like we would read the file here and then it's polite when
173
00:08:00,040 --> 00:08:03,920
we're done with the file to actually call close on that file.
174
00:08:03,920 --> 00:08:06,460
But sometimes it's difficult to remember to call close.
175
00:08:06,460 --> 00:08:09,160
So there's actually a better way to open a file.
176
00:08:09,160 --> 00:08:14,120
Instead of assigning a file variable like that, we can actually pass a block to the
177
00:08:14,120 --> 00:08:15,480
open method.
178
00:08:15,480 --> 00:08:19,280
The block will get as a block parameter the file object.
179
00:08:19,280 --> 00:08:21,100
So open opens the file.
180
00:08:21,100 --> 00:08:24,540
Then it hands us that file handle in the file variable here.
181
00:08:24,540 --> 00:08:27,000
And then inside of here we can read our movies.
182
00:08:27,000 --> 00:08:31,560
And the really cool thing about using a block here is the block will take care of closing
183
00:08:31,560 --> 00:08:35,620
the file when we're done with whatever we're doing with the movies.
184
00:08:35,620 --> 00:08:37,039
So what do we want to do inside of this method?
185
00:08:37,039 --> 00:08:38,860
Well, we can use this file object.
186
00:08:38,860 --> 00:08:43,960
And there are several iterator based methods available on file objects and other IO objects
187
00:08:43,960 --> 00:08:44,960
in Ruby.
188
00:08:44,960 --> 00:08:47,200
And one of those is each line.
189
00:08:47,200 --> 00:08:53,200
That's an iterator that iterates through each line of the file and it gives it a block parameter
190
00:08:53,200 --> 00:08:54,200
to our block.
191
00:08:54,200 --> 00:08:56,840
And we can just print out that line.
192
00:08:56,840 --> 00:09:02,680
And if we run this, we should see our three movies, Goonies, Ghostbusters, and Goldfinger.
193
00:09:02,680 --> 00:09:05,860
But there's actually a slightly easier way to do this in Ruby.
194
00:09:05,860 --> 00:09:09,440
Instead of calling open and then iterating through each line, we could actually just
195
00:09:09,440 --> 00:09:11,960
call the read lines method.
196
00:09:11,960 --> 00:09:16,120
Read lines is going to read all the lines of that file into an array.
197
00:09:16,120 --> 00:09:20,480
We know that we can iterate through an array using each.
198
00:09:20,480 --> 00:09:24,520
This is going to yield to us each line because it's just iterating through the array of
199
00:09:24,520 --> 00:09:28,640
lines and we can print out the line that way.
200
00:09:28,640 --> 00:09:29,840
Just like that.
201
00:09:29,840 --> 00:09:31,600
Run that and we get the same thing.
202
00:09:31,600 --> 00:09:33,040
Okay, so cool.
203
00:09:33,040 --> 00:09:37,260
Now we can read the lines, but we still need to parse out the name from the rank.
204
00:09:37,260 --> 00:09:40,560
We need to put the two things into fields or something.
205
00:09:40,560 --> 00:09:41,960
Ruby's really good at this sort of thing.
206
00:09:41,960 --> 00:09:44,560
So let's jump over to IRB and we'll see how to do that.
207
00:09:44,560 --> 00:09:46,080
So here's what our line looks like.
208
00:09:46,080 --> 00:09:48,480
We've got something like Goonies and 10.
209
00:09:48,480 --> 00:09:50,600
Actually, we don't have a space in there.
210
00:09:50,600 --> 00:09:52,880
It's just Goonies and 10 like that.
211
00:09:52,880 --> 00:09:55,360
And we want to split this line into two things.
212
00:09:55,360 --> 00:10:00,960
Well, it may come as no surprise that there's actually a method on a string, line is a string,
213
00:10:00,960 --> 00:10:05,560
called split and then it takes a string, what delimiter we want to split on.
214
00:10:05,560 --> 00:10:10,880
In this case, we want to split across a comma and it returns to us an array of two elements,
215
00:10:10,880 --> 00:10:12,520
Goonies and 10.
216
00:10:12,520 --> 00:10:18,000
So then we could just say, okay, well, the name is in position zero and the rank is in
217
00:10:18,000 --> 00:10:20,020
position one like that.
218
00:10:20,020 --> 00:10:21,920
Just like arrays we're already familiar with.
219
00:10:21,920 --> 00:10:22,920
Yeah, exactly.
220
00:10:22,920 --> 00:10:24,920
But there's actually an easier way to do this.
221
00:10:24,920 --> 00:10:29,040
We can have two variables on the left-hand side, title and rank.
222
00:10:29,040 --> 00:10:35,000
We can call line.split on that comma and Ruby will go ahead and assign, it'll take that
223
00:10:35,000 --> 00:10:37,640
array apart and it'll assign the two parts to the variable.
224
00:10:37,640 --> 00:10:42,320
So if we look at title, we have Goonies and we have rank, we have 10.
225
00:10:42,320 --> 00:10:43,540
So let's put all this together.
226
00:10:43,540 --> 00:10:47,820
If we come back over to our files, what we actually want to do is create movie objects
227
00:10:47,820 --> 00:10:48,820
out of here.
228
00:10:48,820 --> 00:10:51,720
So just up here, I'm going to require our movie file like that.
229
00:10:51,720 --> 00:10:55,680
And instead of just printing out the line, when we read through all the lines, I'm going
230
00:10:55,680 --> 00:10:58,520
to take the title and the rank, just like we did in IRB.
231
00:10:58,520 --> 00:11:04,600
I'm going to split the line across the comma and then we can create a new movie, object,
232
00:11:04,600 --> 00:11:09,720
movie.new, pass in the title, pass in the rank.
233
00:11:09,720 --> 00:11:13,560
We're going to convert the rank to an integer because that's what a rank should be in a
234
00:11:13,560 --> 00:11:14,780
movie.
235
00:11:14,780 --> 00:11:18,160
And then we can just print out the movie like that.
236
00:11:18,160 --> 00:11:22,000
Run that and we have our 2S method being called on our movie.
237
00:11:22,000 --> 00:11:24,920
So we've got our three movie objects just the way we want.
238
00:11:24,920 --> 00:11:28,280
So now let's take this code and actually put it over in our playlist class.
239
00:11:28,280 --> 00:11:29,640
I'm just going to copy it out of here.
240
00:11:29,640 --> 00:11:33,959
I'm going to go over to playlist and I'm just going to do this at the top just so we can
241
00:11:33,959 --> 00:11:34,959
see it.
242
00:11:34,959 --> 00:11:39,560
I'm going to define a method called load and it's going to take a file name.
243
00:11:39,560 --> 00:11:40,760
I'm going to call it from file.
244
00:11:40,760 --> 00:11:44,000
We're just going to paste in this code just like that.
245
00:11:44,000 --> 00:11:49,720
So we're going to read all the lines of whatever file gets passed in, from file.
246
00:11:49,720 --> 00:11:52,760
We're going to create a movie object just as we did before.
247
00:11:52,760 --> 00:11:57,160
But instead of printing out the movie here, we're going to call our add movie method and
248
00:11:57,160 --> 00:12:00,520
we're going to add that movie right into our playlist.
249
00:12:00,520 --> 00:12:07,840
Now over in our flix.rb file, instead of adding the movies this way, we're calling that explicitly,
250
00:12:07,840 --> 00:12:12,240
we can actually get rid of that and we'll just call playlist load and we'll give it
251
00:12:12,240 --> 00:12:17,560
the name movies.csv.
252
00:12:17,560 --> 00:12:22,800
Save that away and then we can jump out to the command line, get out of IRB here and
253
00:12:22,800 --> 00:12:25,520
we should be able to run flix.rb.
254
00:12:25,520 --> 00:12:26,520
How many viewings?
255
00:12:26,520 --> 00:12:27,520
One.
256
00:12:27,520 --> 00:12:30,080
And we see that we have all of our movies being played in the playlist.
257
00:12:30,080 --> 00:12:32,160
They were all read from that file.
258
00:12:32,160 --> 00:12:39,040
Now what if I have multiple movie files like I have movies.csv, Batman movies, csv, movies
259
00:12:39,040 --> 00:12:41,120
with Tom Hanks, csv.
260
00:12:41,120 --> 00:12:42,600
I see where you're going with this.
261
00:12:42,600 --> 00:12:46,800
So we want to be able to pass in a file name from the command line instead of hard coding
262
00:12:46,800 --> 00:12:47,800
this movies.csv thing.
263
00:12:47,800 --> 00:12:48,800
Right.
264
00:12:48,800 --> 00:12:49,800
Right.
265
00:12:49,800 --> 00:12:50,800
We can do that.
266
00:12:50,800 --> 00:12:53,280
So over in flix.rb, we don't want to hard code this name.
267
00:12:53,280 --> 00:12:56,020
We want that to be our default input file name.
268
00:12:56,020 --> 00:12:59,520
But we also want to be able to take a file name from the command line.
269
00:12:59,520 --> 00:13:05,120
And command line arguments are stored in a global variable in Ruby called argv.
270
00:13:05,120 --> 00:13:09,320
If there's one argument on there, we're actually going to call shift which will remove it from
271
00:13:09,320 --> 00:13:11,680
that array of command line arguments.
272
00:13:11,680 --> 00:13:14,400
And that way it won't be used later on in the program.
273
00:13:14,400 --> 00:13:16,480
Then we're going to use this or constructs.
274
00:13:16,480 --> 00:13:21,320
We're going to say, okay, if the argv array has anything in it, then go ahead and return
275
00:13:21,320 --> 00:13:23,040
it to us and use it.
276
00:13:23,040 --> 00:13:28,280
Otherwise, if shift returns nil, meaning we didn't pass in an argument, then go ahead
277
00:13:28,280 --> 00:13:30,200
and use the default movies.csv.
278
00:13:30,200 --> 00:13:31,200
Okay.
279
00:13:31,200 --> 00:13:36,680
So let's create another file of movies here.
280
00:13:36,680 --> 00:13:37,680
We'll call this our...
281
00:13:37,680 --> 00:13:40,079
Let's call it superheroes.
282
00:13:40,079 --> 00:13:41,079
Superhero movies.csv.
283
00:13:41,079 --> 00:13:42,079
All right.
284
00:13:42,079 --> 00:13:45,079
What do you want in here?
285
00:13:45,079 --> 00:13:46,079
That's all.
286
00:13:46,079 --> 00:13:51,640
Let's do Batman, Spider-Man, Superman.
287
00:13:51,640 --> 00:13:52,640
Superman.
288
00:13:52,640 --> 00:13:53,640
Superman.
289
00:13:53,640 --> 00:13:54,640
Superman will get a rank of eight.
290
00:13:54,640 --> 00:13:55,640
All right.
291
00:13:55,640 --> 00:13:56,640
We've got that file.
292
00:13:56,640 --> 00:13:57,640
Let's go back to our command line.
293
00:13:57,640 --> 00:13:58,640
All right.
294
00:13:58,640 --> 00:14:00,120
We can quit out of here.
295
00:14:00,120 --> 00:14:05,479
So now if we run flix.rb and we don't give it any input here, well, then it's just going
296
00:14:05,479 --> 00:14:07,520
to use Goonies, Ghostbusters, and Goldfinger.
297
00:14:07,520 --> 00:14:10,560
That's what's in movies.csv.
298
00:14:10,560 --> 00:14:16,920
But if we run it and we pass in the name of a file, let's say it's the superhero movies.csv,
299
00:14:16,920 --> 00:14:20,120
well then it's going to use our superhero movies.
300
00:14:20,120 --> 00:14:24,480
So now we can just pass in any valid file name and create movies and add a new playlist
301
00:14:24,480 --> 00:14:26,760
in any custom way we want.
302
00:14:26,760 --> 00:14:29,040
Now wasn't there one more request that you got?
303
00:14:29,040 --> 00:14:30,040
There was.
304
00:14:30,040 --> 00:14:31,040
We need to turn this inside out.
305
00:14:31,040 --> 00:14:37,160
We want to save off the movies and their final rankings to a file, something like this.
306
00:14:37,160 --> 00:14:38,640
So let's just try something here.
307
00:14:38,640 --> 00:14:42,920
Suppose we have these three movies and they're all inside of an array and we want to create
308
00:14:42,920 --> 00:14:46,719
a movie rankings file with them ordered from highest to lowest.
309
00:14:46,719 --> 00:14:49,319
So we've seen that we can open a file.
310
00:14:49,319 --> 00:14:51,240
In this case, we want to open a file.
311
00:14:51,240 --> 00:14:55,839
We'll call it movie rankings.csv.
312
00:14:55,839 --> 00:14:59,760
And a little bit different than we opened a file before, we want to give it a mode.
313
00:14:59,760 --> 00:15:00,760
We're going to open it for writing.
314
00:15:00,760 --> 00:15:02,360
We're going to write to this file.
315
00:15:02,360 --> 00:15:06,959
So we give it a W. And then that takes a block.
316
00:15:06,960 --> 00:15:09,120
And then we're going to get the file variable here.
317
00:15:09,120 --> 00:15:13,400
And then what we want to do is we want to loop through all the movies.
318
00:15:13,400 --> 00:15:15,120
We'll go ahead and sort them because we can do that.
319
00:15:15,120 --> 00:15:17,640
We'll loop through all the movies using an iterator.
320
00:15:17,640 --> 00:15:20,380
And then for each of those movies, we're going to use our file handle.
321
00:15:20,380 --> 00:15:22,920
We can call put as on that file.
322
00:15:22,920 --> 00:15:24,240
And then we're just going to print out a string.
323
00:15:24,240 --> 00:15:31,200
It's going to be movie title, comma, movie.rank, just like we would expect the CSV format to
324
00:15:31,200 --> 00:15:32,200
be.
325
00:15:32,200 --> 00:15:38,480
So if we go ahead and run that now, nothing happens, but if we look back, we go back to
326
00:15:38,480 --> 00:15:43,360
terminal here, we see we've got a movie rankings.csv file.
327
00:15:43,360 --> 00:15:47,200
I'll just use the cat command and we'll print it out like that.
328
00:15:47,200 --> 00:15:51,400
And we see that we've got our movies saved off in a CSV format.
329
00:15:51,400 --> 00:15:53,600
So let's come back to this code.
330
00:15:53,600 --> 00:15:54,760
And it's pretty good code.
331
00:15:54,760 --> 00:15:56,140
Let's go ahead and take it out of here.
332
00:15:56,140 --> 00:15:57,600
We'll go back to our playlist.
333
00:15:57,600 --> 00:16:04,240
We're going to find a new method inside of playlist, we'll just put it right below load.
334
00:16:04,240 --> 00:16:05,520
We're going to call this one save.
335
00:16:05,520 --> 00:16:07,600
We're going to call it save.
336
00:16:07,600 --> 00:16:09,680
And it's going to take a to file.
337
00:16:09,680 --> 00:16:11,400
We could even put a default on there if we want to.
338
00:16:11,400 --> 00:16:15,000
Let's say the default file that we're going to save to is movierankings.csv.
339
00:16:15,000 --> 00:16:18,360
We're going to paste in the code that we just had.
340
00:16:18,360 --> 00:16:23,760
We're going to change this hard coded string over to to file.
341
00:16:23,760 --> 00:16:27,120
And then instead of movies, we're going to use our at movies because that's our instance
342
00:16:27,120 --> 00:16:29,040
variable inside of this file.
343
00:16:29,040 --> 00:16:34,680
And then over in flix.rb again at the bottom, when we're all done, we're just going to call
344
00:16:34,680 --> 00:16:37,280
playlist.save to call that method.
345
00:16:37,280 --> 00:16:40,260
And we'll go ahead and use the default file name there.
346
00:16:40,260 --> 00:16:41,260
So let's try that out.
347
00:16:41,260 --> 00:16:42,840
We'll come back to the console.
348
00:16:42,840 --> 00:16:45,280
I'm going to remove movierankings.csv.
349
00:16:45,280 --> 00:16:48,240
I'm going to run ruby flix.rb there.
350
00:16:48,240 --> 00:16:51,040
Go ahead and run three viewings.
351
00:16:51,040 --> 00:16:56,880
And then when I quit, if we have a look, if we look at movierankings.csv, we've got all
352
00:16:56,880 --> 00:16:58,120
of our rankings in there.
353
00:16:58,120 --> 00:16:59,120
All right.
354
00:16:59,120 --> 00:17:02,520
I think we've made everybody happy in terms of our feature requests.
355
00:17:02,520 --> 00:17:07,800
But I see some areas where we could use a little cleanup.
356
00:17:07,800 --> 00:17:08,800
Yeah.
357
00:17:08,800 --> 00:17:10,579
There are a few areas of the code that we could definitely clean up.
358
00:17:10,579 --> 00:17:12,339
So let's do that.
359
00:17:12,339 --> 00:17:16,880
So back over in this save method we just wrote, when we're trying to convert this movie over
360
00:17:16,880 --> 00:17:22,359
to a CSV format and then sending it in to put us, it should be nice to encapsulate this
361
00:17:22,359 --> 00:17:23,599
as some method.
362
00:17:23,599 --> 00:17:27,280
It looks like it should be on movie because we're getting the movie's title and the movie's
363
00:17:27,280 --> 00:17:28,280
rank.
364
00:17:28,280 --> 00:17:29,280
So let's just take that out.
365
00:17:29,280 --> 00:17:34,320
What we'd rather do is just take the movie object and call something like toCSV because
366
00:17:34,320 --> 00:17:37,120
we have a toS method that converts it to a string.
367
00:17:37,120 --> 00:17:41,000
How about a toCSV method that converts it to CSV format?
368
00:17:41,000 --> 00:17:44,520
So we'll go over to our movie and I'll just define that.
369
00:17:44,520 --> 00:17:47,360
I'll just do it up here so it's nice at the top.
370
00:17:47,360 --> 00:17:48,360
Call it toCSV.
371
00:17:48,360 --> 00:17:50,360
We'll just paste in that code.
372
00:17:50,360 --> 00:17:52,439
It's just going to return a string.
373
00:17:52,440 --> 00:17:56,720
We don't need this movie object here because we're inside of a movie instance.
374
00:17:56,720 --> 00:18:01,600
We're just going to use our instance variables at title and at rank.
375
00:18:01,600 --> 00:18:05,600
And that way if we ever need to convert a movie to CSV, we can just call this toCSV
376
00:18:05,600 --> 00:18:06,600
method.
377
00:18:06,600 --> 00:18:08,520
So back over to our playlist.
378
00:18:08,520 --> 00:18:10,380
We've got that one cleaned up a little bit.
379
00:18:10,380 --> 00:18:12,440
Up in the load method we've got something similar.
380
00:18:12,440 --> 00:18:13,800
It's kind of the reverse situation.
381
00:18:13,800 --> 00:18:17,960
We're having this line that's comma separated and then we want to create a movie object
382
00:18:17,960 --> 00:18:20,160
from that and then we add it to the playlist.
383
00:18:20,160 --> 00:18:22,560
So it's kind of doing a lot of stuff here.
384
00:18:22,560 --> 00:18:27,120
So we'd like to be able to take this parsing code out of here and then just have a method
385
00:18:27,120 --> 00:18:29,960
that will create a movie from a line.
386
00:18:29,960 --> 00:18:32,440
So we kind of want something like this.
387
00:18:32,440 --> 00:18:33,920
We want to add a movie.
388
00:18:33,920 --> 00:18:38,080
We're not going to have a movie object, but we'd like to be able to call something like,
389
00:18:38,080 --> 00:18:42,760
you know normally we call movie.new to create a new movie, but what if we had a method called
390
00:18:42,760 --> 00:18:48,760
fromCSV and then it took that line, it would parse the line and then create a new movie
391
00:18:48,760 --> 00:18:50,360
object for us.
392
00:18:50,360 --> 00:18:55,160
So then we could take this code, cut that out of there.
393
00:18:55,160 --> 00:18:58,400
Over in our movie class again, we can define that.
394
00:18:58,400 --> 00:18:59,760
I'll just do it up here.
395
00:18:59,760 --> 00:19:06,320
Now normally we would define this as fromCSV, but remember we don't have a movie object
396
00:19:06,320 --> 00:19:11,200
to call this on and we need an object to call an instance method like this.
397
00:19:11,200 --> 00:19:16,320
So instead we saw with modules where we could define methods using self.
398
00:19:16,320 --> 00:19:20,320
And that gave us a module method that then we could call directly on the module.
399
00:19:20,320 --> 00:19:26,879
Well using self.here we'll create a class method, which means we can call fromCSV directly
400
00:19:26,879 --> 00:19:31,540
on the movie class, which is exactly what we want because we don't have an object.
401
00:19:31,540 --> 00:19:34,639
So all we need to do in here, split the line.
402
00:19:34,639 --> 00:19:37,080
Actually this method has to take the line in as a parameter.
403
00:19:37,080 --> 00:19:41,560
We're just going to split the line and then we're going to create a new movie based on
404
00:19:41,560 --> 00:19:43,320
the title and the rank.
405
00:19:43,320 --> 00:19:47,960
One other little thing I want to do right here is this toI method that gets called.
406
00:19:47,960 --> 00:19:54,280
If the number in that file isn't, if it's actually not a number, if it's a string or
407
00:19:54,280 --> 00:19:58,520
something like that, somebody didn't put in a proper rank, then this is going to convert
408
00:19:58,520 --> 00:20:00,200
whatever that is to zero.
409
00:20:00,200 --> 00:20:04,340
We'd rather it raise an exception because if there's not a number in there, it's really
410
00:20:04,340 --> 00:20:06,120
problematic for our program.
411
00:20:06,120 --> 00:20:11,240
So instead we can use this method integer like that and pass in the rank.
412
00:20:11,240 --> 00:20:15,120
And that way if the rank isn't actually an integer, we'll know it straight away because
413
00:20:15,120 --> 00:20:16,120
it will get an exception.
414
00:20:16,120 --> 00:20:22,020
So it's just a little nicer way to do validations on things coming in from files.
415
00:20:22,020 --> 00:20:27,000
So back over to our playlist, we're calling fromCSV on the movie class, we're calling
416
00:20:27,000 --> 00:20:30,160
toCSV on a movie object.
417
00:20:30,160 --> 00:20:31,840
So let's go try that out.
418
00:20:31,840 --> 00:20:36,440
Over here we can run Ruby, Flix.rb and then we say how many viewings?
419
00:20:36,440 --> 00:20:37,440
One.
420
00:20:37,440 --> 00:20:38,440
All right.
421
00:20:38,440 --> 00:20:40,720
Our movies are still being read in from our input file.
422
00:20:40,720 --> 00:20:46,400
We'll do quit and then if we have a look, we should have movie rankings and sure enough
423
00:20:46,400 --> 00:20:49,720
it wrote them out in CSV format as well.
424
00:20:49,720 --> 00:20:53,480
So now that you know the ins and outs of Ruby, you can add some IO to your game.
425
00:20:53,480 --> 00:20:58,040
When you're done with the exercise, your players or the users of your game will be able to
426
00:20:58,040 --> 00:21:01,160
run the game with an arbitrary number of iterations.
427
00:21:01,160 --> 00:21:06,000
They'll be able to read custom player files, custom players from a file, and then they'll
428
00:21:06,000 --> 00:21:08,560
be able to save the high scores off to a file.
429
00:21:08,560 --> 00:21:11,399
And in the next section we'll learn about inheritance and we're not talking about all
430
00:21:11,399 --> 00:21:15,000
those family heirlooms you're going to get handed down from your parents.
431
00:21:15,000 --> 00:21:39,080
See you then.
37242
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.