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,200
We want to take a moment to show you something kind of cool.
2
00:00:06,200 --> 00:00:10,040
Now it may seem a little computer science-y at this point, but a lot of folks ask about
3
00:00:10,040 --> 00:00:11,040
it.
4
00:00:11,040 --> 00:00:14,760
Now we're going to revisit this again later, so if it doesn't all make sense now, don't
5
00:00:14,760 --> 00:00:16,000
worry about it.
6
00:00:16,000 --> 00:00:18,440
Now we told you that methods are always called on an object.
7
00:00:18,440 --> 00:00:22,320
In this example, we're calling the capitalize method on the movie object.
8
00:00:22,320 --> 00:00:25,280
We sometimes say that the movie is an explicit receiver.
9
00:00:25,280 --> 00:00:26,280
But what's up with putAs?
10
00:00:26,280 --> 00:00:30,080
Well, it's a method, but we've been calling it without a receiver.
11
00:00:30,080 --> 00:00:32,000
So which object are we calling it on?
12
00:00:32,000 --> 00:00:36,140
Okay, I've opened up a new program file here just so we can see what's going on.
13
00:00:36,140 --> 00:00:39,440
And we've seen that we can use putAs to print a string to the console.
14
00:00:39,440 --> 00:00:43,600
So I'm going to use the string, who's my receiver.
15
00:00:43,600 --> 00:00:47,120
And we know when we run that, we get who's my receiver.
16
00:00:47,120 --> 00:00:49,320
So the question is, what is this putAs thing?
17
00:00:49,320 --> 00:00:50,920
Well, it's actually a method.
18
00:00:50,920 --> 00:00:53,820
In fact, the string is a parameter to that method.
19
00:00:53,820 --> 00:00:58,800
And we could put the parameter in parentheses, like we've seen with other method calls.
20
00:00:58,800 --> 00:01:02,000
We normally don't do that with putAs, just because it's such a common and
21
00:01:02,000 --> 00:01:05,480
simple command that we can leave out the parentheses in Ruby.
22
00:01:05,480 --> 00:01:08,200
But this shows us that putAs is a method.
23
00:01:08,200 --> 00:01:10,960
We run that, and we get the same thing.
24
00:01:10,960 --> 00:01:14,760
But the real question is, what object are we calling this method on?
25
00:01:14,760 --> 00:01:17,200
So we wanna know, what is this object over here?
26
00:01:17,200 --> 00:01:19,440
Because we know that every time we call a method,
27
00:01:19,440 --> 00:01:22,600
there must be an object that receives that method call.
28
00:01:22,600 --> 00:01:23,680
So here's the thing.
29
00:01:23,680 --> 00:01:26,480
Anytime there's no object on the left-hand side,
30
00:01:26,480 --> 00:01:28,880
Ruby implicitly uses an object.
31
00:01:28,880 --> 00:01:32,680
That object is referenced by the variable called self.
32
00:01:32,680 --> 00:01:35,760
And self always references the current object.
33
00:01:35,760 --> 00:01:39,760
So anytime you don't see an explicit receiver, the thing on the left-hand side
34
00:01:39,760 --> 00:01:43,160
of the dot, then there's always an implicit or default receiver.
35
00:01:43,160 --> 00:01:48,160
And that receiver is the object that's referenced by the variable self.
36
00:01:48,160 --> 00:01:49,880
Now self always has a value, and
37
00:01:49,880 --> 00:01:52,280
it changes depending on where we are in a program.
38
00:01:52,280 --> 00:01:55,280
We'll see how those change when we look at classes and methods later.
39
00:01:55,280 --> 00:01:59,840
But right now, let's see what the value of self is inside of our main program.
40
00:01:59,840 --> 00:02:02,280
I'm gonna have to take this off, cuz that's not gonna be valid syntax.
41
00:02:02,280 --> 00:02:05,440
I can just do putS, self, that's the variable name.
42
00:02:05,440 --> 00:02:09,520
If we run that, we see that self is this thing called main.
43
00:02:09,520 --> 00:02:11,360
That's the name of self.
44
00:02:11,360 --> 00:02:13,600
So let's see what the class of self is.
45
00:02:17,520 --> 00:02:19,960
It's actually an object.
46
00:02:19,960 --> 00:02:22,840
So in the top level scope of a main program like this,
47
00:02:22,840 --> 00:02:26,840
Ruby automatically arranges things so that there's this global object,
48
00:02:26,840 --> 00:02:29,760
referenced by self, and it's called main.
49
00:02:29,760 --> 00:02:31,200
Then when we call putS,
50
00:02:31,200 --> 00:02:34,600
it's automatically used as the receiver of that method call.
51
00:02:34,600 --> 00:02:38,000
It's as if we typed self. right there.
52
00:02:38,000 --> 00:02:39,760
Unfortunately, this won't work.
53
00:02:39,760 --> 00:02:42,640
It would kinda be nice if it did from an instructional standpoint.
54
00:02:42,640 --> 00:02:45,200
But if we try to run this program now,
55
00:02:45,200 --> 00:02:48,920
we see that we get this error private method putS called for object.
56
00:02:48,920 --> 00:02:52,799
Well, it turns out that putS is actually a private method.
57
00:02:52,799 --> 00:02:56,720
And the way that Ruby enforces private methods is that we can't have an
58
00:02:56,720 --> 00:02:59,679
explicit receiver for private methods.
59
00:02:59,679 --> 00:03:02,119
So the takeaway here is that putS is a method.
60
00:03:02,119 --> 00:03:04,200
It's actually being called on an object.
61
00:03:04,200 --> 00:03:08,040
That object is stored in self, and we don't have to have an explicit receiver
62
00:03:08,040 --> 00:03:12,160
because in this top level program, self is already set up for us.
63
00:03:12,160 --> 00:03:13,160
Now don't let this throw you.
64
00:03:13,160 --> 00:03:16,519
The important thing is that every method has been called on an object,
65
00:03:16,519 --> 00:03:18,560
even if you don't see that object.
66
00:03:18,560 --> 00:03:21,040
If you understand that for now, then you're set.
67
00:03:21,040 --> 00:03:24,320
Yeah, and now that we know how to call some of Ruby's built-in methods,
68
00:03:24,320 --> 00:03:27,400
in the next section, we'll look at how to write a method of our own.
69
00:03:27,400 --> 00:03:49,400
See you then.
6069
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.