All language subtitles for pragstudio-ruby-03-numbers-strings (Transcribed on 27-Apr-2023 20-41-49)

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified) Download
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
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,520 Now, every game needs a player, so that's where we're going to start. 2 00:00:07,520 --> 00:00:11,760 The first thing we want to do is print out a player's name and initial health value. 3 00:00:11,760 --> 00:00:14,520 And to do that, we need to learn about numbers and strings. 4 00:00:14,520 --> 00:00:18,040 Now, you'll use these in every Ruby program you write, so it's as good a place as any 5 00:00:18,040 --> 00:00:19,760 to start learning. 6 00:00:19,760 --> 00:00:24,280 So in the exercise, your goal is to print out something like, Larry's health is 60. 7 00:00:24,280 --> 00:00:29,240 Now, the exercise would be pretty boring if we showed you exactly how to print that out. 8 00:00:29,240 --> 00:00:32,360 So instead, we're going to look at a similar Ruby example. 9 00:00:32,360 --> 00:00:37,120 We'll use numbers and strings to print out something like, Goonies has a rank of 10. 10 00:00:37,120 --> 00:00:40,360 And then you can do something similar for the Larry player. 11 00:00:40,360 --> 00:00:42,120 So let's look at numbers in IRB. 12 00:00:42,120 --> 00:00:45,760 Okay, so let's go back over to IRB here. 13 00:00:45,760 --> 00:00:47,200 And we'll just start playing around with some numbers. 14 00:00:47,200 --> 00:00:48,840 A great place to do that. 15 00:00:48,840 --> 00:00:52,620 So numbers that don't have decimal points in Ruby are integers. 16 00:00:52,620 --> 00:00:55,720 So let's just say we had 7 times 3. 17 00:00:55,720 --> 00:00:59,680 That's a fairly respectable score for your favorite football team, maybe. 18 00:00:59,680 --> 00:01:04,379 Or if we wanted to use floating point numbers, floating point numbers have decimal points. 19 00:01:04,379 --> 00:01:11,480 So 499 and add 199, and we get 698, which is sort of like the grand total on a convenience 20 00:01:11,480 --> 00:01:13,360 store bill or something like that. 21 00:01:13,360 --> 00:01:17,720 So pretty much what you expect, you've got floating point numbers with decimal points. 22 00:01:17,720 --> 00:01:21,759 You've got integers or fixed numbers without decimal points. 23 00:01:21,760 --> 00:01:27,040 Something that's a little unexpected to be aware of is if you do arithmetic on two integer 24 00:01:27,040 --> 00:01:32,060 values, here we're gonna divide 25 by 2, we get back an integer value 12. 25 00:01:32,060 --> 00:01:33,560 You may not expect that. 26 00:01:33,560 --> 00:01:39,000 But if you mix types here, we're gonna have 25, we're gonna divide it by 2.0. 27 00:01:39,000 --> 00:01:41,000 So we've got a floating point type here. 28 00:01:41,000 --> 00:01:44,440 Then we're gonna get back 12.5, which is what we expect. 29 00:01:44,440 --> 00:01:49,160 So the long and the short of this is that Ruby will automatically return the more general 30 00:01:49,160 --> 00:01:50,160 type. 31 00:01:50,160 --> 00:01:52,119 It's the floating point type. 32 00:01:52,119 --> 00:01:55,259 So let's go ahead and assign a number to a variable. 33 00:01:55,259 --> 00:01:58,960 And variables in Ruby just spring into existence when we assign to them. 34 00:01:58,960 --> 00:02:02,360 And we're just gonna assign 10 times 2, which will be 20. 35 00:02:02,360 --> 00:02:07,080 So a variable here is just a way for us to hang onto that number, and then we can reference 36 00:02:07,080 --> 00:02:08,139 it later. 37 00:02:08,139 --> 00:02:09,840 We could also have demerits. 38 00:02:09,840 --> 00:02:11,200 And we're gonna use floating point types here. 39 00:02:11,200 --> 00:02:15,220 We're gonna have 0.5 times 3 demerits. 40 00:02:15,220 --> 00:02:16,960 And then we're gonna use another variable. 41 00:02:16,960 --> 00:02:24,080 We'll have our rank, and we're gonna assign to our rank the merits minus the demerits. 42 00:02:24,080 --> 00:02:26,280 And we get back 18.5. 43 00:02:26,280 --> 00:02:31,240 So we can use the variables once we've assigned to them to get whatever the number back is. 44 00:02:31,240 --> 00:02:36,840 In this case, we mixed the two of them, so we got back a floating point type, 18.5. 45 00:02:36,840 --> 00:02:41,480 Now as a hint of something to come a little bit later, all numbers in Ruby are objects, 46 00:02:41,480 --> 00:02:44,560 which means that they respond to messages. 47 00:02:44,560 --> 00:02:51,200 So if we have a look at the class of merits, for example, we see that it's a fixnum. 48 00:02:51,200 --> 00:02:57,500 If we look at demerits, as for its class, it's a floating point type. 49 00:02:57,500 --> 00:02:58,760 So Mike, this is great. 50 00:02:58,760 --> 00:03:02,240 We now know how to represent the movie's rank as an integer. 51 00:03:02,240 --> 00:03:04,720 The next thing we need to know is the name. 52 00:03:04,720 --> 00:03:10,040 Now name is just a sequence of characters like Goonies, and in Ruby, that's just a string. 53 00:03:10,040 --> 00:03:11,760 Now strings come in two flavors. 54 00:03:11,760 --> 00:03:15,040 They come as double quoted and single quoted. 55 00:03:15,040 --> 00:03:18,239 So I'm thinking we looked at numbers in IRB. 56 00:03:18,239 --> 00:03:19,880 Let's look at strings in a program file. 57 00:03:19,880 --> 00:03:23,560 Yeah, it'll work better in a Ruby program file because we'll be able to print things 58 00:03:23,560 --> 00:03:26,519 out inside of there, so let's give that a shot. 59 00:03:26,519 --> 00:03:27,799 So let's play it around with strings. 60 00:03:27,799 --> 00:03:32,320 We'll get out of IRB here, and I'm going to create a new Ruby program file. 61 00:03:32,320 --> 00:03:37,600 I think we're going to call the program file we use for our game, actually for our movies. 62 00:03:37,600 --> 00:03:38,760 I'm going to call it flix.rb. 63 00:03:38,760 --> 00:03:40,560 I think that's good. 64 00:03:40,560 --> 00:03:41,560 Yeah. 65 00:03:41,560 --> 00:03:44,000 So let's start with a really simple single quoted string. 66 00:03:44,000 --> 00:03:47,840 We're going to print this out to the command line or to the console, and we're going to 67 00:03:47,840 --> 00:03:51,720 say, Mikey loves Goonies. 68 00:03:51,720 --> 00:03:53,480 So we're just using put s again. 69 00:03:53,480 --> 00:03:55,320 We're using a single quoted string here. 70 00:03:55,320 --> 00:03:59,360 If we run that, we've got Mikey loves Goonies. 71 00:03:59,360 --> 00:04:02,400 But I'm sort of the possessive type here, so what if I wanted to print out in a single 72 00:04:02,400 --> 00:04:07,640 quoted string, Mikey's favorite movie is Goonies? 73 00:04:07,640 --> 00:04:12,839 Well, Mikey is the star of Goonies, so no wonder it's his favorite movie. 74 00:04:12,839 --> 00:04:14,200 He is the star of Goonies. 75 00:04:14,200 --> 00:04:15,200 You're absolutely right. 76 00:04:15,200 --> 00:04:16,200 Yeah. 77 00:04:16,200 --> 00:04:20,200 Well, we see when we do that, we get some weird coloration in this syntax, and that's 78 00:04:20,200 --> 00:04:25,240 because when we use a single quoted string, it began here, but Ruby thinks it ended right 79 00:04:25,240 --> 00:04:26,240 there. 80 00:04:26,240 --> 00:04:29,479 So the way to get around this in a single quoted string is to use something called an 81 00:04:29,479 --> 00:04:31,039 escape sequence. 82 00:04:31,039 --> 00:04:36,159 So escape sequence here is just to use a backslash, and you notice that the string turns all green, 83 00:04:36,160 --> 00:04:39,200 and Ruby now knows, oh, okay, that's not the end of the string after all. 84 00:04:39,200 --> 00:04:43,440 So we can go ahead and print that out, and sure enough, we've got Mikey's favorite movie 85 00:04:43,440 --> 00:04:44,960 is Goonies. 86 00:04:44,960 --> 00:04:48,960 So I might change my favorite movie from time to time, so let me assign the movie to a variable. 87 00:04:48,960 --> 00:04:50,480 I'm going to use the variable movie. 88 00:04:50,480 --> 00:04:54,720 I'll use a single quoted string for Goonies there, and then I want to print out the string 89 00:04:54,720 --> 00:05:03,840 again, Mikey's, and I'm going to use an apostrophe s there, favorite movie is, and then I want 90 00:05:03,840 --> 00:05:09,919 to tack on the name of that variable, and I can do that in a single quoted string. 91 00:05:09,919 --> 00:05:14,039 I can concatenate two strings together using the plus operator, so then I just give it 92 00:05:14,039 --> 00:05:17,960 the name of the variable, and because the name or the variable, in this case, movie 93 00:05:17,960 --> 00:05:21,640 points to a string, and we've got a string on the left-hand side as well, then we're 94 00:05:21,640 --> 00:05:23,640 just going to get a full string. 95 00:05:23,640 --> 00:05:25,280 Mikey's favorite movie is Goonies. 96 00:05:25,280 --> 00:05:28,640 All right, so now we're ready to complete our task in this section. 97 00:05:28,640 --> 00:05:30,280 We know numbers, we know strings. 98 00:05:30,280 --> 00:05:33,000 Yeah, so let's put out the movie name and its rank. 99 00:05:33,000 --> 00:05:34,000 Perfect. 100 00:05:34,000 --> 00:05:35,080 All right, so we'll have a new variable. 101 00:05:35,080 --> 00:05:39,720 We'll call it rank, and we'll set it to 10, and then we'll use put s, and now I want to 102 00:05:39,720 --> 00:05:42,640 take that movie variable because I've got my movie name in there. 103 00:05:42,640 --> 00:05:48,800 I'm going to concatenate onto that, has a rank of, I want to space there, I'm going 104 00:05:48,800 --> 00:05:52,640 to concatenate then the actual rank value. 105 00:05:52,640 --> 00:05:53,640 So let's go ahead and run that. 106 00:05:53,640 --> 00:05:54,640 I'm going to save the file. 107 00:05:54,640 --> 00:05:56,080 We're going to run it. 108 00:05:56,080 --> 00:05:57,080 Oh. 109 00:05:57,080 --> 00:05:59,400 Yeah, we get our first error here. 110 00:05:59,400 --> 00:06:03,039 Can't convert fixnum into string on line nine. 111 00:06:03,039 --> 00:06:05,440 So let's close this off, go back to line nine. 112 00:06:05,440 --> 00:06:07,239 Sure enough, it's the last line we typed there. 113 00:06:07,239 --> 00:06:12,200 Well, the issue here is rank is an integer, and we're trying to concatenate that onto 114 00:06:12,200 --> 00:06:15,440 a string, and Ruby needs a little bit of help here. 115 00:06:15,440 --> 00:06:17,520 It needs this rank to be a string. 116 00:06:17,520 --> 00:06:22,159 So as we saw previously, numbers are objects in Ruby, and they have methods, and one of 117 00:06:22,159 --> 00:06:28,159 those methods is the to underscore s method, which will convert this fixnum integer into 118 00:06:28,160 --> 00:06:31,840 a string, and that way we're concatenating all these strings together. 119 00:06:31,840 --> 00:06:35,920 If we ran that now, sure enough, we get, Goonies has a rank of 10. 120 00:06:35,920 --> 00:06:40,960 Okay, now we can avoid some of this escaping and this concatenating by using double quoted 121 00:06:40,960 --> 00:06:41,960 strings. 122 00:06:41,960 --> 00:06:46,320 Yeah, let's just convert all the single quoted strings we have now into double quoted strings, 123 00:06:46,320 --> 00:06:47,320 and we'll just see what the difference is. 124 00:06:47,320 --> 00:06:48,320 See what it looks like. 125 00:06:48,320 --> 00:06:49,320 Sure. 126 00:06:49,320 --> 00:06:52,000 Okay, so I'm just going to comment out all these single quoted strings, and we'll just 127 00:06:52,000 --> 00:06:53,980 work these off line by line. 128 00:06:53,980 --> 00:06:56,120 We'll convert them to double quoted strings. 129 00:06:56,120 --> 00:06:57,580 So the first one here is pretty simple. 130 00:06:57,580 --> 00:07:00,780 It's just a straightforward single quoted string. 131 00:07:00,780 --> 00:07:01,780 No real change here. 132 00:07:01,780 --> 00:07:03,820 We're just going to use double quotes. 133 00:07:03,820 --> 00:07:06,380 It wouldn't really matter if you use single quotes or double quotes here. 134 00:07:06,380 --> 00:07:11,320 There's no escaping going on, no concatenation happening, and if we run that, no surprise 135 00:07:11,320 --> 00:07:14,000 we get, Mikey loves Goonies. 136 00:07:14,000 --> 00:07:15,440 So let's tackle the second one. 137 00:07:15,440 --> 00:07:18,840 A little bit more interesting because we've got this escape sequence in here. 138 00:07:18,840 --> 00:07:24,060 I'm going to change this to a double quoted string, and you notice that Ruby's okay with 139 00:07:24,060 --> 00:07:25,280 the syntax highlighting here. 140 00:07:25,280 --> 00:07:26,620 It doesn't change. 141 00:07:26,620 --> 00:07:30,760 So we can actually just take off that escape sequence because Ruby's not going to be confused. 142 00:07:30,760 --> 00:07:33,440 The string starts here and it ends here. 143 00:07:33,440 --> 00:07:36,800 That little quote in the middle won't make any difference at all, but it cleans up the 144 00:07:36,800 --> 00:07:37,800 string nicely. 145 00:07:37,800 --> 00:07:41,440 So if we run that, Mikey's favorite movie is Goonies. 146 00:07:41,440 --> 00:07:42,840 Okay. 147 00:07:42,840 --> 00:07:44,120 So let's tackle the third one here. 148 00:07:44,120 --> 00:07:46,500 I'm just going to take out those comments. 149 00:07:46,500 --> 00:07:47,800 We have a variable. 150 00:07:47,800 --> 00:07:51,520 We want to basically substitute that variable into the string, and with a single quoted 151 00:07:51,520 --> 00:07:54,560 string here, we're doing it with concatenation. 152 00:07:54,560 --> 00:07:56,920 Well double quoted strings makes this a little bit easier. 153 00:07:56,920 --> 00:07:59,580 We saw we could take off this escape sequence. 154 00:07:59,580 --> 00:08:04,160 Now at the end, instead of using the plus operator to concatenate, we'll actually take 155 00:08:04,160 --> 00:08:11,760 this variable and we'll surround it by hash and curly braces, just like that. 156 00:08:11,760 --> 00:08:17,080 And this little syntax, hash, curly brace, and then the ending curly brace here, any 157 00:08:17,080 --> 00:08:20,000 Ruby expression can live inside of that little syntax. 158 00:08:20,000 --> 00:08:23,600 And what Ruby does is it evaluates whatever's in there. 159 00:08:23,600 --> 00:08:27,920 Right now it's a variable that points to a string Goonies, evaluates that code, and then 160 00:08:27,920 --> 00:08:31,200 it just substitutes the result right back into the string. 161 00:08:31,200 --> 00:08:35,440 So if we run that, sure enough, we get the same thing as we did before, but now we're 162 00:08:35,440 --> 00:08:38,280 using a variable inside of the string. 163 00:08:38,280 --> 00:08:42,840 So now to the last one, where we have our rank and our movie variable all in the same 164 00:08:42,840 --> 00:08:45,800 variable or all in the same string in this case. 165 00:08:45,800 --> 00:08:48,280 So I'm just going to use a double quoted string here. 166 00:08:48,280 --> 00:08:50,880 We can surround this with this syntax. 167 00:08:50,880 --> 00:08:56,200 We often call this interpolation because we're going to interpolate the value of movie and 168 00:08:56,200 --> 00:08:58,240 substitute it back in the string. 169 00:08:58,240 --> 00:09:00,680 We don't have to use the concatenation there. 170 00:09:00,680 --> 00:09:05,560 Come out over here, our rank, we want to surround that because we want to replace its value 171 00:09:05,560 --> 00:09:07,080 inside of the string as well. 172 00:09:07,080 --> 00:09:08,680 Just put a double quote at the end. 173 00:09:08,680 --> 00:09:10,560 So we've got a double quoted string. 174 00:09:10,560 --> 00:09:15,640 Now something kind of cool is when you use this interpolation syntax, for example, rank, 175 00:09:15,640 --> 00:09:20,080 even though rank is an integer, it needs to be converted into a string. 176 00:09:20,080 --> 00:09:25,360 Well when you use this syntax, any expression inside of this syntax will automatically be 177 00:09:25,360 --> 00:09:27,200 converted over to a string. 178 00:09:27,200 --> 00:09:30,360 So we don't even have to call this 2S method. 179 00:09:30,360 --> 00:09:34,680 So we take that off, we run our program, and we've got all those fixed. 180 00:09:34,680 --> 00:09:36,200 Goonies has a rank of 10. 181 00:09:36,200 --> 00:09:41,360 So we've seen how to substitute n values, basically concatenating, but in a much simpler 182 00:09:41,360 --> 00:09:46,080 form and a cleaner form, and also how to do escape sequences. 183 00:09:46,080 --> 00:09:49,000 So say Mike, what if we wanted to multiply the rank by two? 184 00:09:49,000 --> 00:09:51,160 Yeah, so you really like Goonies in this case. 185 00:09:51,160 --> 00:09:52,160 I really like Goonies. 186 00:09:52,160 --> 00:09:53,160 You doubly like that. 187 00:09:53,160 --> 00:09:57,240 Okay, well, inside of here, we can run any Ruby expression. 188 00:09:57,240 --> 00:09:59,600 So we can multiply the rank by two. 189 00:09:59,600 --> 00:10:04,320 And if we ran that, then we're going to get Goonies has a rank of 20. 190 00:10:04,320 --> 00:10:09,460 Double quoted strings also support a number of other escape sequences that single quoted 191 00:10:09,460 --> 00:10:10,560 strings don't. 192 00:10:10,560 --> 00:10:14,280 For example, if we use a backslash n, we would get a new line there. 193 00:10:14,280 --> 00:10:17,880 If we use something like a backslash t, we'll get a tab there. 194 00:10:17,880 --> 00:10:21,520 And if we run it, we see we've got our tab and our new line. 195 00:10:21,520 --> 00:10:24,520 So that's the difference between the two different types of strings. 196 00:10:24,520 --> 00:10:27,880 It really boils down to how much processing Ruby does with the string. 197 00:10:27,880 --> 00:10:30,960 It does a bit more processing with double quoted strings because it has to be able to 198 00:10:30,960 --> 00:10:34,760 substitute values and support more escape sequences. 199 00:10:34,760 --> 00:10:38,560 And you can think of single quoted strings as basically just simpler forms of strings 200 00:10:38,560 --> 00:10:40,700 or dumber strings, if you will. 201 00:10:40,700 --> 00:10:44,960 In general, I use double quoted strings pretty much everywhere, unless I just have a simple 202 00:10:44,960 --> 00:10:46,080 word or something. 203 00:10:46,080 --> 00:10:51,160 That way I can go back and add escape sequences or do more substitutions later if I want to. 204 00:10:51,160 --> 00:10:52,200 All right. 205 00:10:52,200 --> 00:10:54,480 So now you know all about numbers and strings. 206 00:10:54,480 --> 00:10:56,200 You're ready to go back to your game. 207 00:10:56,200 --> 00:10:59,600 In the exercise, you're going to print out the player's name and their health. 208 00:10:59,600 --> 00:11:00,600 Yeah. 209 00:11:00,600 --> 00:11:02,920 And in the next section, we're going to come back and we're going to talk about how to 210 00:11:02,920 --> 00:11:06,960 call methods on strings so you can start off your game with a little bit more style. 211 00:11:06,960 --> 00:11:16,880 Yeah, some style. 19301

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.