Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
We want to take a moment to show you something kind of cool.
Now it may seem a little computer science-y at this point, but a lot of folks ask about
it.
Now we're going to revisit this again later, so if it doesn't all make sense now, don't
worry about it.
Now we told you that methods are always called on an object.
In this example, we're calling the capitalize method on the movie object.
We sometimes say that the movie is an explicit receiver.
But what's up with putAs?
Well, it's a method, but we've been calling it without a receiver.
So which object are we calling it on?
Okay, I've opened up a new program file here just so we can see what's going on.
And we've seen that we can use putAs to print a string to the console.
So I'm going to use the string, who's my receiver.
And we know when we run that, we get who's my receiver.
So the question is, what is this putAs thing?
Well, it's actually a method.
In fact, the string is a parameter to that method.
And we could put the parameter in parentheses, like we've seen with other method calls.
We normally don't do that with putAs, just because it's such a common and
simple command that we can leave out the parentheses in Ruby.
But this shows us that putAs is a method.
We run that, and we get the same thing.
But the real question is, what object are we calling this method on?
So we wanna know, what is this object over here?
Because we know that every time we call a method,
there must be an object that receives that method call.
So here's the thing.
Anytime there's no object on the left-hand side,
Ruby implicitly uses an object.
That object is referenced by the variable called self.
And self always references the current object.
So anytime you don't see an explicit receiver, the thing on the left-hand side
of the dot, then there's always an implicit or default receiver.
And that receiver is the object that's referenced by the variable self.
Now self always has a value, and
it changes depending on where we are in a program.
We'll see how those change when we look at classes and methods later.
But right now, let's see what the value of self is inside of our main program.
I'm gonna have to take this off, cuz that's not gonna be valid syntax.
I can just do putS, self, that's the variable name.
If we run that, we see that self is this thing called main.
That's the name of self.
So let's see what the class of self is.
It's actually an object.
So in the top level scope of a main program like this,
Ruby automatically arranges things so that there's this global object,
referenced by self, and it's called main.
Then when we call putS,
it's automatically used as the receiver of that method call.
It's as if we typed self. right there.
Unfortunately, this won't work.
It would kinda be nice if it did from an instructional standpoint.
But if we try to run this program now,
we see that we get this error private method putS called for object.
Well, it turns out that putS is actually a private method.
And the way that Ruby enforces private methods is that we can't have an
explicit receiver for private methods.
So the takeaway here is that putS is a method.
It's actually being called on an object.
That object is stored in self, and we don't have to have an explicit receiver
because in this top level program, self is already set up for us.
Now don't let this throw you.
The important thing is that every method has been called on an object,
even if you don't see that object.
If you understand that for now, then you're set.
Yeah, and now that we know how to call some of Ruby's built-in methods,
in the next section, we'll look at how to write a method of our own.
See you then.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.