1
00:00:00,080 --> 00:00:08,060
In this lesson, I want to show you some methods that are available on integer objects in Ruby just
2
00:00:08,060 --> 00:00:14,630
to showcase that the way that we invoke a method is going to be consistent regardless of the type of
3
00:00:14,630 --> 00:00:16,360
object that we are working with.
4
00:00:16,370 --> 00:00:20,480
So for example, here is the integer ten, and that is not a string.
5
00:00:20,480 --> 00:00:23,180
That is a totally different type of object.
6
00:00:23,180 --> 00:00:29,570
It is an integer, but methods work the exact same way as they do on strings, because everything in
7
00:00:29,570 --> 00:00:33,380
Ruby is an object and objects are going to have different methods.
8
00:00:33,380 --> 00:00:39,440
So, for example, if I want to invoke a method on this integer, it's as simple as putting a dot and
9
00:00:39,440 --> 00:00:41,600
then putting a valid method name.
10
00:00:41,600 --> 00:00:48,230
So for example, there is a method called next and that is going to give us a number one higher than
11
00:00:48,230 --> 00:00:50,540
the number that the method is invoked upon.
12
00:00:50,540 --> 00:00:54,650
So the next value after ten and that is going to give us 11.
13
00:00:54,650 --> 00:01:00,390
So the return value of the next method is a new integer object storing the value.
14
00:01:00,390 --> 00:01:00,960
11.
15
00:01:00,990 --> 00:01:02,130
Here's another example.
16
00:01:02,130 --> 00:01:07,470
If I take negative one and call next on that, we're going to get the value zero.
17
00:01:07,500 --> 00:01:08,970
Same exact principle.
18
00:01:09,000 --> 00:01:10,500
We're starting with an object.
19
00:01:10,500 --> 00:01:16,680
We're using the dot syntax to invoke a method that basically means running a procedure or asking the
20
00:01:16,680 --> 00:01:18,330
object to do something for us.
21
00:01:18,330 --> 00:01:23,820
And that operation will typically produce a return value like the ones we see on the right hand side.
22
00:01:23,820 --> 00:01:26,310
And that's going to be another object of its own.
23
00:01:26,310 --> 00:01:31,440
That's why everything is object oriented, because we're starting with an object, a data structure,
24
00:01:31,440 --> 00:01:35,940
we're interacting with it, and we're producing a new object, a new data structure.
25
00:01:35,940 --> 00:01:37,110
So that is next.
26
00:01:37,110 --> 00:01:44,100
And there's actually going to be a method that does the exact same thing, and that is called successor,
27
00:01:44,100 --> 00:01:46,260
which is abbreviated as CC.
28
00:01:46,530 --> 00:01:47,880
That is short for successor.
29
00:01:47,880 --> 00:01:52,080
It's just an alias, which means an alternate name for the same method.
30
00:01:52,080 --> 00:01:53,730
So this will do the exact same thing.
31
00:01:53,730 --> 00:01:56,270
It's going to give us the next value in sequence.
32
00:01:56,280 --> 00:02:04,680
Now if you want to get the number one before there is a similar method called predecessor, Pred is
33
00:02:04,680 --> 00:02:08,430
its shortened name and that will give you a number one lower.
34
00:02:08,430 --> 00:02:09,960
So here we can see nine.
35
00:02:09,960 --> 00:02:11,940
Right here is the predecessor of ten.
36
00:02:11,940 --> 00:02:18,000
And if I take, for example, negative one and call the predecessor method on that, we're going to
37
00:02:18,000 --> 00:02:18,980
get negative two.
38
00:02:18,990 --> 00:02:24,960
So this is basically the exact same thing as either adding plus one or subtracting minus one.
39
00:02:24,960 --> 00:02:28,380
And those are both valid ways that we can arrive at the same result.
40
00:02:28,380 --> 00:02:34,320
But this is thinking about that exact same problem from a method oriented approach, just to get us
41
00:02:34,320 --> 00:02:36,330
thinking about what exactly a method is.
42
00:02:36,360 --> 00:02:37,650
It's just the command.
43
00:02:37,650 --> 00:02:42,270
It's an instruction to a given object to do something or to produce something.
44
00:02:42,270 --> 00:02:48,720
And methods work the exact same way on integers as they do on strings and as they do on every other
45
00:02:48,720 --> 00:02:51,840
type of object that we're going to be exploring throughout the course.
46
00:02:51,840 --> 00:02:55,950
So hopefully that gives you a better sense of exactly what an object method is.
47
00:02:55,980 --> 00:02:58,170
And I will see you in the next lesson.