All language subtitles for pragstudio-ruby-04-variables-objects (Transcribed on 24-Apr-2023 20-58-19)

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

Alright, now in the last exercise you've established your first player.

You used one variable to hold its name and another to hold its health.

But we're going to need more than one player in our game.

Yeah, okay.

So that also means that we're going to need to use more variables.

In the upcoming exercise, your objective will be to create more players and print them out

so it'll look something like this.

But again, we don't want to spoil the fun for you.

So we're going to continue with our movie theme and leave the game to you.

So our objective will be to create more movies and print them out like this using variables.

Now understanding variables and how they work is really important.

So let's take a moment just to step back and look at variables in a bit more depth.

This will also give us an opportunity to start looking at calling methods on objects.

Okay, so let's hop into an IRB session again.

And we need a variable here.

So let's create a variable movie.

We're going to use our old friend, the Goonies.

So what we've done here is assigned this string Goonies to the variable movies.

And then when we reference movies at any time, then it just holds onto that string.

So we use the string literal here to create that string object.

Now when we assign the variable, it just springs into existence.

There's no need to declare any types in Ruby.

Ruby is a dynamically typed language.

So right now, the movie's type, if you will, is the string object.

So we've seen that we can use variables inside of other strings.

So we can say Mikey's favorite movie is, and we're inside of a double quoted string here,

so I can use the interpolation syntax.

And we've got Mikey's favorite movie as Goonies again.

We can also reassign to variables.

So now let's say I change my mind, my favorite movie is now Ghostbusters, right?

So I've changed the variable and now points to a different string object.

And if I rerun this double quoted string, well, now I have Mikey's favorite movie is

Ghostbusters because that movie variable holds onto a string object.

In fact, if we look at the class of movie, we can see that it was created from the string

class.

We've also seen that variables can point to numbers.

So let's try a few more of those.

Let's say we're doing some ranking system.

So we have a number of thumbs ups.

Let's say we've got ten thumbs ups, okay?

And then we've got maybe, oh, I don't know, two thumbs downs, right?

So the overall rank could be, we've seen part of this before, thumbs up minus thumbs down,

just like that.

So now we've got a rank of eight.

And if we look at the class of rank, it is a fixed num.

So we're dealing with a fixed num object here.

We also saw that we could create times in Ruby.

In this case, I'm gonna assign it to the variable current time.

It's gonna hold onto a new time object.

And then we could use that current time variable inside of another string.

We could say like the time is, and then substitute that in, current time, just like that.

So we have a variable, oh, and in this case, if you look at current time's class, well,

it's a time object.

So we've seen how we can use variables to store strings, we can use variables to store

numbers, and we can use variables to store any object type.

In the last case, we used a time object.

Now, it's important to remember that variables hold references to objects, not the objects

themselves.

Right.

You could think of a variable like a pointer to an object, and the object just lives somewhere

in memory.

Right.

Well, let's look at an example of that.

Yeah, let's look at an example.

Okay, back in IRB, I'm just gonna clean up the screen a little bit here.

We're still in an IRB session.

I'm gonna create a new variable here.

I'm gonna call it my favorite movie, and yes, I'm gonna assign Goonies to that variable.

So we've got my favorite movie.

Now I'm gonna create your favorite movie.

That's a little presumptuous.

Yeah, but I know you like Goonies too, so I'm gonna assign my favorite movie to your

favorite movie.

So we've basically aliased one variable with another.

Your favorite movie points to my favorite movie, which is Goonies.

So let's have a look.

So if I look at my favorite movie, we can call the method object ID.

That variable points to that object.

It's just the number of the object in memory.

If we look at your favorite movie's object ID, well, it's the same object.

And that's because we've got two variables here.

They both point to the same object.

It's this string Goonies that lives in memory somewhere.

So just to show that a little bit different way, what if I have my favorite movie and

I change the first character?

And I can do that with a string just by indexing to the zeroth character there.

And I'm gonna change that first character to an L.

Now if I print out my favorite movie, it's Looney's, which is actually a movie name.

And if I print out your favorite movie, oh, it's Looney's as well, because both of those

variables point to the same object.

I changed that object to the Looney's string.

So both your favorite movie and my favorite movie is now Looney's.

I'm tired of being tied to you.

I want my favorite movie to be Ghostbusters.

Well, we can do that.

I'll change my favorite movie to be Ghostbusters.

Okay, now my favorite movie is Ghostbusters.

Your favorite movie is still Looney's, however.

So now we have two variables, my favorite movie, your favorite movie, and two objects,

two string objects.

So let's see that in slow motion.

My favorite movie references a string object, Goonies.

When we assign my favorite movie to your favorite movie, your favorite movie now references

the same string object, Goonies.

Note that a new string object wasn't created here.

We have one string object and two variables.

Now when we change the first character of my favorite movie to L, both variables get

changed to Looney's.

Finally, we say we assign my favorite movie to a new string object, Ghostbusters.

The variable my favorite movie now references a different string object, but your favorite

movie still points to Looney's.

So now we have two string objects and two variables.

So now we know that when we use a variable, we're really using an object.

Yeah, and on one hand, a string object is nothing more than a sequence of characters,

but objects can also do things.

And we tell an object to do something by calling a method on that object.

So sometimes you hear this referred to as sending the object a message.

So let's give that a whirl.

So to call a method, we need an object.

We're going to go back to our favorite movie object here.

And then we can just call a method by using a dot syntax.

We take the object and then we call the method.

In this case, we want to get the length of that string and it's seven characters long.

So maybe we want to reverse the string.

Well, we can call the reverse method on that object and that's just going to return a new

string object in its reverse sequence here.

So Goonies reversed.

Synog.

Yeah.

Does it sound like the next Academy Award winning film?

You never know what an Academy Award winning film is going to be named, right?

So that's a method that doesn't take any parameters, but methods can also take parameters.

So let's look at a method like center, which is going to center a string between a certain

number of characters.

And we pass in that number of characters as a parameter.

They go between parentheses.

So this is going to center a movie.

Actually, I want it about 10 characters.

So it centers Goonies within a 10 character string.

So that's one parameter being passed to a method.

There are also methods that take multiple parameters.

One of those methods is the ljust method.

And ljust takes the first parameter is how long we want the string to be, let's say 30

characters, and then what the character should be used for padding that.

So we just separate the two parameters by a comma there, and we get back a left justified

string padded with dots in this case.

Now numbers are objects too, so we can call methods on numbers in Ruby.

So here's a number, rank equals eight.

And then we can call, we've already seen we can call the 2s method that converts it to

a string.

We could call something like the 2f method that converts it to a float, or we might,

if we wanted to convert it to an integer, which it already is, but there's also a 2i

method on those.

So we've got our fixnum eight there.

Now you may be used to doing multiplication or any sort of arithmetic on numbers.

Say we take the rank and we multiply it by two.

And that looks to be like something special.

It doesn't look like a method call.

But in reality, what's happening underneath is Ruby is actually calling a method called

times.

It's actually the operator, the times operator, and then it's passing in the parameter of

two.

So we get 16.

So this form here is really just a shortcut for calling the method.

We've also seen how to create time objects.

So let me create another time object here, time.new.

And time objects have methods as well.

For example, we can take our current time and ask it what is the current hour.

It's the 16th hour and the current minute.

So let's recap what's going on here.

Suppose we have a variable called movie that points to a string object.

If we want to reverse it, we can call the reverse method.

The reverse method doesn't take any parameters.

It returns a new string in reverse order.

String objects also have a method called center, which does take one parameter.

Parameters come after the method name and are surrounded by parentheses.

Methods can also take multiple parameters.

For example, the ljust method we just saw takes two parameters.

And when you pass in multiple parameters, you separate them with a comma.

Here's the takeaway.

You always call methods on an object using the dot notation.

The object, sometimes called the receiver of the message, is always on the left.

And the method name and parameters, or the message being sent, is always on the right.

Now you might be asking yourself, how would I know that the center method takes one parameter

and the ljust method takes two parameters?

Right.

Or let's say what if I want to capitalize a movie, or what if I just want to know what

else I can do with a string object?

Yeah, the string object has a boatload of methods on it.

And if you look at the Ruby standard library as a whole, I think there's over 9,000 methods.

We need to teach them how to fish.

It's time to go fishing.

So I'm over at a terminal here.

I've got out of IRB.

And the command I want to show you is ri.

Now this comes with Ruby.

When you install Ruby, you get the ri command.

And it's just a command line utility for viewing documentation.

So let's say I wanted to find all the documentation for the string class.

I can just say ri string.

And it gives me a little explanation of what a string does.

And if I hit the space bar, I can sort of scroll through this.

You see here all the instance methods, methods that I can call on string objects.

And we've used some of those already in here.

We got the length, for example.

We reversed the string.

I can keep scrolling back down.

And then we get out of ri.

So if you know of a particular method you want to find out about, you can use ri, give

the class name string.

And let's say we're looking for some information about how to center a string.

Just type in the method name center.

And then it pulls up just the documentation for the center method.

And it's kind of handy.

It shows you you have your string object you call center.

It takes two parameters here.

The second parameter is optional in this case.

And returns a new string object.

But what I really like about the ri documentation are these little examples down here.

Because they're executable examples.

You can actually copy one of these out.

I'm just going to copy that.

I'm going to get out of that ri session, go into an IRB session where we can run some

code.

I can just paste in that example.

And sure enough, it's running code so I can see how center works.

Yeah, that is so helpful for experimenting.

Just trying things out.

Looking in the documentation, jumping out to IRB, and seeing what it does and playing

with it.

Yeah, and we can just change the example here.

You might want to see, okay, well, what happens if I use an asterisk?

Sure enough, it pads it that way.

So it's a really good way, as you said, to get feedback about how methods work.

And then we can get back out of IRB.

And then let's say we wanted to look at other center methods.

So if I type in just center, the method name, well, it's only defined in one class string.

So I'm going to get the documentation for the center method on string.

But if I were to try something like reverse, which is another method we use.

Well, reverse is defined on multiple classes in Ruby.

It's defined in array.

It's defined in this class called ip adder.

And then finally, it's defined in string.

So you can either go for the whole class and then find the method you want.

You can type in a class and a specific method.

Or you could just try a generic method and see where it's defined.

All right, so this is great.

Now we know how to find out what an object can do and how to call those methods.

But you may have noticed if you were really paying attention, some of those methods had

a question mark or an exclamation point.

We should take a look at those.

Yeah, let's look at a few of those.

So one of those methods was the empty method on the string class.

And the name of the method was empty with a question mark at the end.

And if we look at that, we see that we have a string we call empty question mark and it

returns true or false.

So this is purely convention, but methods that end in a question mark are often called

predicate methods because they return a true or a false value.

You can almost think of them as like asking a question.

We're asking the string, are you empty?

So let's try this in IRB.

We'll create a variable with an empty string that's just double quotes.

We'll call movie.empty question mark and we get back true.

If we were to change movie to our favorite and we call empty again, it returns false.

So empty question mark returns true or false.

Let's try a couple more of those.

We also have a method called start with and the question mark is part of the method name

and then we can give it a character.

So does Goonies start with G?

Yes it does.

We can also call something like include question mark.

Does Goonies include the character S?

No it doesn't.

So that's all there is to predicate methods.

Methods that have a question mark at the end, it's part of the method name and then just

return true or false.

Now how about those methods that end with an exclamation point?

Yeah, so let me get it out of IRB.

One of those methods that we saw, well one method we've used is the reverse method on

strings and we saw that reverse, call it on a string object and it returns a new string

here.

In fact the documentation says returns a new string with the characters from string in

reverse order.

But there's another method, string reverse exclamation point.

The exclamation point is part of the method name.

If we look at the documentation for it, you notice that we're calling it on the string

object str, but it's also returning the string object.

So it's not creating a new string.

And in fact the documentation says reverses the string in place.

So let's look at that in IRB.

In fact I'm going to go to the top of the screen here.

I'm going to have a movie, say this is Ghostbusters.

We've seen we can call movie.reverse that reverses the string.

And if we look at the original object movie here, it hasn't changed.

It's still Ghostbusters.

But what happens if we call reverse with an exclamation point at the end?

We get the name of a German movie.

Another Academy Award winning movie.

So it's reversed the string, but what happens if we look at the original object now?

Well, it's been reversed in place.

Those original characters were also reversed.

So these methods that end in an exclamation point, when you see those, just think that

they might do something slightly unexpected or slightly dangerous.

In this case, by calling reverse with an exclamation point, we reverse the original string.

So it's just something to be aware of when you use those methods.

So I think we're now ready to put this all together.

If you'll recall, our initial objective was to print out a movie listing, something like

this.

You know, Ruby is really good at string formatting chores like this.

And by chaining a few string methods together, the code for this ends up being quite elegant.

So here's what we need to do.

You capitalize the movie title, left justify it, and include the rank.

So let's do this with Ghostbusters.

Okay.

So back over in IRB, let's set ourselves up a movie.

And I'm going to use the lowercase form of Ghostbusters here because part of our objective

is to actually capitalize it.

And I want to assign the capitalize movie to a variable.

I'm going to call it title.

I'm going to call movie.capitalize.

That's one of the methods that's on the string class.

And we have back Ghostbusters in the capitalize form.

So we have our first thing done.

Yeah, we've got the first thing done.

The second thing is just to left justify it, right?

So we know how to do an L just.

We'll do it 30 padded with dots, just like that.

So we got the second thing done.

We're so far so good, yeah?

Right.

So we did this in two separate steps.

We first capitalized the movie, assigned it the title, and then we left justified that

title.

We can do this in one fell swoop if we want to.

We can set our title variable, take movie, call capitalize.

Capitalize returns a string.

So we just have a string object right here at this point in the code.

So we can just turn around and call left justify or L just on it and left justified that way.

So we're chaining together the capitalize and the L just methods.

And we have our title all formatted the way we want.

So now we just need the rank.

We'll have a rank variable.

We'll set it to nine.

And then we know how to put these two things together.

We can concatenate them as a single quoted string or we can use a double quoted string,

which I'll do here.

And we'll just have our title and then a space and then our rank in the double quoted string.

And now we've got our formatted movie listing.

Give yourself a pat on the back.

You learned a lot in this section.

You learned about variables and assignment.

You learned about how to call methods both with and without parameters.

You learned how to access the docs and predicate methods.

You even learned how to chain methods together.

So now you know everything you need to know to format how your players are printed when

you start the game.

And now you can give it a little style.

So give that a go in the exercise.

And when we come back, we'll clear up some of the mystery around the put as method.

We'll see you then.

My favorite movie is your favorite.

Wait a second.

My favorite movie is your favorite.

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