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

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
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
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

Now, every game needs a player, so that's where we're going to start.

The first thing we want to do is print out a player's name and initial health value.

And to do that, we need to learn about numbers and strings.

Now, you'll use these in every Ruby program you write, so it's as good a place as any

to start learning.

So in the exercise, your goal is to print out something like, Larry's health is 60.

Now, the exercise would be pretty boring if we showed you exactly how to print that out.

So instead, we're going to look at a similar Ruby example.

We'll use numbers and strings to print out something like, Goonies has a rank of 10.

And then you can do something similar for the Larry player.

So let's look at numbers in IRB.

Okay, so let's go back over to IRB here.

And we'll just start playing around with some numbers.

A great place to do that.

So numbers that don't have decimal points in Ruby are integers.

So let's just say we had 7 times 3.

That's a fairly respectable score for your favorite football team, maybe.

Or if we wanted to use floating point numbers, floating point numbers have decimal points.

So 499 and add 199, and we get 698, which is sort of like the grand total on a convenience

store bill or something like that.

So pretty much what you expect, you've got floating point numbers with decimal points.

You've got integers or fixed numbers without decimal points.

Something that's a little unexpected to be aware of is if you do arithmetic on two integer

values, here we're gonna divide 25 by 2, we get back an integer value 12.

You may not expect that.

But if you mix types here, we're gonna have 25, we're gonna divide it by 2.0.

So we've got a floating point type here.

Then we're gonna get back 12.5, which is what we expect.

So the long and the short of this is that Ruby will automatically return the more general

type.

It's the floating point type.

So let's go ahead and assign a number to a variable.

And variables in Ruby just spring into existence when we assign to them.

And we're just gonna assign 10 times 2, which will be 20.

So a variable here is just a way for us to hang onto that number, and then we can reference

it later.

We could also have demerits.

And we're gonna use floating point types here.

We're gonna have 0.5 times 3 demerits.

And then we're gonna use another variable.

We'll have our rank, and we're gonna assign to our rank the merits minus the demerits.

And we get back 18.5.

So we can use the variables once we've assigned to them to get whatever the number back is.

In this case, we mixed the two of them, so we got back a floating point type, 18.5.

Now as a hint of something to come a little bit later, all numbers in Ruby are objects,

which means that they respond to messages.

So if we have a look at the class of merits, for example, we see that it's a fixnum.

If we look at demerits, as for its class, it's a floating point type.

So Mike, this is great.

We now know how to represent the movie's rank as an integer.

The next thing we need to know is the name.

Now name is just a sequence of characters like Goonies, and in Ruby, that's just a string.

Now strings come in two flavors.

They come as double quoted and single quoted.

So I'm thinking we looked at numbers in IRB.

Let's look at strings in a program file.

Yeah, it'll work better in a Ruby program file because we'll be able to print things

out inside of there, so let's give that a shot.

So let's play it around with strings.

We'll get out of IRB here, and I'm going to create a new Ruby program file.

I think we're going to call the program file we use for our game, actually for our movies.

I'm going to call it flix.rb.

I think that's good.

Yeah.

So let's start with a really simple single quoted string.

We're going to print this out to the command line or to the console, and we're going to

say, Mikey loves Goonies.

So we're just using put s again.

We're using a single quoted string here.

If we run that, we've got Mikey loves Goonies.

But I'm sort of the possessive type here, so what if I wanted to print out in a single

quoted string, Mikey's favorite movie is Goonies?

Well, Mikey is the star of Goonies, so no wonder it's his favorite movie.

He is the star of Goonies.

You're absolutely right.

Yeah.

Well, we see when we do that, we get some weird coloration in this syntax, and that's

because when we use a single quoted string, it began here, but Ruby thinks it ended right

there.

So the way to get around this in a single quoted string is to use something called an

escape sequence.

So escape sequence here is just to use a backslash, and you notice that the string turns all green,

and Ruby now knows, oh, okay, that's not the end of the string after all.

So we can go ahead and print that out, and sure enough, we've got Mikey's favorite movie

is Goonies.

So I might change my favorite movie from time to time, so let me assign the movie to a variable.

I'm going to use the variable movie.

I'll use a single quoted string for Goonies there, and then I want to print out the string

again, Mikey's, and I'm going to use an apostrophe s there, favorite movie is, and then I want

to tack on the name of that variable, and I can do that in a single quoted string.

I can concatenate two strings together using the plus operator, so then I just give it

the name of the variable, and because the name or the variable, in this case, movie

points to a string, and we've got a string on the left-hand side as well, then we're

just going to get a full string.

Mikey's favorite movie is Goonies.

All right, so now we're ready to complete our task in this section.

We know numbers, we know strings.

Yeah, so let's put out the movie name and its rank.

Perfect.

All right, so we'll have a new variable.

We'll call it rank, and we'll set it to 10, and then we'll use put s, and now I want to

take that movie variable because I've got my movie name in there.

I'm going to concatenate onto that, has a rank of, I want to space there, I'm going

to concatenate then the actual rank value.

So let's go ahead and run that.

I'm going to save the file.

We're going to run it.

Oh.

Yeah, we get our first error here.

Can't convert fixnum into string on line nine.

So let's close this off, go back to line nine.

Sure enough, it's the last line we typed there.

Well, the issue here is rank is an integer, and we're trying to concatenate that onto

a string, and Ruby needs a little bit of help here.

It needs this rank to be a string.

So as we saw previously, numbers are objects in Ruby, and they have methods, and one of

those methods is the to underscore s method, which will convert this fixnum integer into

a string, and that way we're concatenating all these strings together.

If we ran that now, sure enough, we get, Goonies has a rank of 10.

Okay, now we can avoid some of this escaping and this concatenating by using double quoted

strings.

Yeah, let's just convert all the single quoted strings we have now into double quoted strings,

and we'll just see what the difference is.

See what it looks like.

Sure.

Okay, so I'm just going to comment out all these single quoted strings, and we'll just

work these off line by line.

We'll convert them to double quoted strings.

So the first one here is pretty simple.

It's just a straightforward single quoted string.

No real change here.

We're just going to use double quotes.

It wouldn't really matter if you use single quotes or double quotes here.

There's no escaping going on, no concatenation happening, and if we run that, no surprise

we get, Mikey loves Goonies.

So let's tackle the second one.

A little bit more interesting because we've got this escape sequence in here.

I'm going to change this to a double quoted string, and you notice that Ruby's okay with

the syntax highlighting here.

It doesn't change.

So we can actually just take off that escape sequence because Ruby's not going to be confused.

The string starts here and it ends here.

That little quote in the middle won't make any difference at all, but it cleans up the

string nicely.

So if we run that, Mikey's favorite movie is Goonies.

Okay.

So let's tackle the third one here.

I'm just going to take out those comments.

We have a variable.

We want to basically substitute that variable into the string, and with a single quoted

string here, we're doing it with concatenation.

Well double quoted strings makes this a little bit easier.

We saw we could take off this escape sequence.

Now at the end, instead of using the plus operator to concatenate, we'll actually take

this variable and we'll surround it by hash and curly braces, just like that.

And this little syntax, hash, curly brace, and then the ending curly brace here, any

Ruby expression can live inside of that little syntax.

And what Ruby does is it evaluates whatever's in there.

Right now it's a variable that points to a string Goonies, evaluates that code, and then

it just substitutes the result right back into the string.

So if we run that, sure enough, we get the same thing as we did before, but now we're

using a variable inside of the string.

So now to the last one, where we have our rank and our movie variable all in the same

variable or all in the same string in this case.

So I'm just going to use a double quoted string here.

We can surround this with this syntax.

We often call this interpolation because we're going to interpolate the value of movie and

substitute it back in the string.

We don't have to use the concatenation there.

Come out over here, our rank, we want to surround that because we want to replace its value

inside of the string as well.

Just put a double quote at the end.

So we've got a double quoted string.

Now something kind of cool is when you use this interpolation syntax, for example, rank,

even though rank is an integer, it needs to be converted into a string.

Well when you use this syntax, any expression inside of this syntax will automatically be

converted over to a string.

So we don't even have to call this 2S method.

So we take that off, we run our program, and we've got all those fixed.

Goonies has a rank of 10.

So we've seen how to substitute n values, basically concatenating, but in a much simpler

form and a cleaner form, and also how to do escape sequences.

So say Mike, what if we wanted to multiply the rank by two?

Yeah, so you really like Goonies in this case.

I really like Goonies.

You doubly like that.

Okay, well, inside of here, we can run any Ruby expression.

So we can multiply the rank by two.

And if we ran that, then we're going to get Goonies has a rank of 20.

Double quoted strings also support a number of other escape sequences that single quoted

strings don't.

For example, if we use a backslash n, we would get a new line there.

If we use something like a backslash t, we'll get a tab there.

And if we run it, we see we've got our tab and our new line.

So that's the difference between the two different types of strings.

It really boils down to how much processing Ruby does with the string.

It does a bit more processing with double quoted strings because it has to be able to

substitute values and support more escape sequences.

And you can think of single quoted strings as basically just simpler forms of strings

or dumber strings, if you will.

In general, I use double quoted strings pretty much everywhere, unless I just have a simple

word or something.

That way I can go back and add escape sequences or do more substitutions later if I want to.

All right.

So now you know all about numbers and strings.

You're ready to go back to your game.

In the exercise, you're going to print out the player's name and their health.

Yeah.

And in the next section, we're going to come back and we're going to talk about how to

call methods on strings so you can start off your game with a little bit more style.

Yeah, some style.

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