1 00:00:00,000 --> 00:00:02,639 [MUSIC] 2 00:00:02,639 --> 00:00:08,707 [SOUND] In this stage, we're going to learn how to call methods on an object. 3 00:00:08,707 --> 00:00:12,770 To be clear, we're not talking about passing an argument to a method. 4 00:00:12,770 --> 00:00:14,770 We mean taking a piece of data and 5 00:00:14,770 --> 00:00:18,790 calling a method that is only available on that piece of data. 6 00:00:18,790 --> 00:00:22,480 For example, strings have one set of methods available on them, and 7 00:00:22,480 --> 00:00:24,490 numbers have a different set of methods. 8 00:00:25,710 --> 00:00:29,720 Almost every type of data available in Ruby has a large set of methods you 9 00:00:29,720 --> 00:00:30,860 can call on it. 10 00:00:30,860 --> 00:00:32,040 How large? 11 00:00:32,040 --> 00:00:36,780 Any given string is going to have over 170 different methods. 12 00:00:36,780 --> 00:00:38,954 A number is going to have over 130. 13 00:00:39,990 --> 00:00:43,990 Don't worry, we're not going to try and teach you all of those methods right now. 14 00:00:43,990 --> 00:00:47,540 You'll only use a small portion of them on a regular basis. 15 00:00:47,540 --> 00:00:51,280 But it is important to understand how to call methods on an object, and 16 00:00:51,280 --> 00:00:53,100 that's just what this stage will show you. 17 00:00:54,460 --> 00:00:58,570 When you call a method, it's important for Ruby to know which object you're calling 18 00:00:58,570 --> 00:01:02,500 it on, because the behavior of the method will vary based on the object. 19 00:01:02,500 --> 00:01:06,128 We type the dot operator following an object to indicate that we're calling 20 00:01:06,128 --> 00:01:07,430 a method on it. 21 00:01:07,430 --> 00:01:10,280 So here you see a dot operator 22 00:01:10,280 --> 00:01:15,290 that indicates that we're calling the length method on this string, "AA". 23 00:01:15,290 --> 00:01:19,488 Here's another dot operator that indicates we're calling the length method on this 24 00:01:19,488 --> 00:01:20,371 string instead. 25 00:01:20,371 --> 00:01:24,155 And the result of the length method varies for these two different strings, 26 00:01:24,155 --> 00:01:26,653 because obviously they have different lengths. 27 00:01:26,653 --> 00:01:30,700 The first output's a length of 2, the second one output's a length of 9. 28 00:01:30,700 --> 00:01:34,760 And here you see the data operator being used with several numbers. 29 00:01:34,760 --> 00:01:38,450 So we call the odd method on the number 3, and 30 00:01:38,450 --> 00:01:43,330 since 3 is odd and not even, that returns true down here. 31 00:01:43,330 --> 00:01:48,245 By the way, question mark is a valid character at the end of a method name, 32 00:01:48,245 --> 00:01:49,770 and so is exclamation point. 33 00:01:49,770 --> 00:01:53,410 These two symbols are valid only at the very end of a method name. 34 00:01:53,410 --> 00:01:54,810 This feature is unique to Ruby. 35 00:01:54,810 --> 00:01:57,990 It's a little unusual for programming languages. 36 00:01:57,990 --> 00:02:02,170 The question mark doesn't have any special meaning to the Ruby language itself but 37 00:02:02,170 --> 00:02:05,590 by convention, a question mark usually means that a method is going to return 38 00:02:05,590 --> 00:02:06,970 either true or false. 39 00:02:06,970 --> 00:02:09,810 The same is true for this even method down here. 40 00:02:09,810 --> 00:02:12,060 Here, we call the even method on the number 3. 41 00:02:12,060 --> 00:02:13,725 And since 3 is odd and not even, 42 00:02:13,725 --> 00:02:17,130 obviously that's going to return false down here at the output. 43 00:02:18,135 --> 00:02:22,930 Likewise, if we do call dot even on the number 4, well, 44 00:02:22,930 --> 00:02:25,540 4 is even so that is going to return true. 45 00:02:26,710 --> 00:02:30,900 In place of the object itself, we can use a variable that refers to the object. 46 00:02:30,900 --> 00:02:35,890 So if we wanted, instead of saying "AA".length, 47 00:02:35,890 --> 00:02:40,740 we could assign the string "AA" to a variable named string, 48 00:02:40,740 --> 00:02:45,804 and then we can print the length of the value that stored in the string variable. 49 00:02:47,460 --> 00:02:51,730 So let's do that in place of put this "AA".length here, and 50 00:02:51,730 --> 00:02:56,580 get rid of all this, and we can also do the same with a number. 51 00:02:56,580 --> 00:02:58,609 Let's create a variable named, number. 52 00:02:58,609 --> 00:03:03,923 We'll assign it number 4, and we'll call the even method 53 00:03:03,923 --> 00:03:09,010 on the value that's stored in the number variable. 54 00:03:09,010 --> 00:03:10,290 Save that, try running it. 55 00:03:13,077 --> 00:03:14,980 Oops, I made a typo, one second. 56 00:03:17,671 --> 00:03:20,020 You'll notice that it gave me an error down here. 57 00:03:20,020 --> 00:03:25,420 Reporting an undefined method while obviously there's no method name to pute. 58 00:03:25,420 --> 00:03:30,090 So we're gonna change that to puts, try rerunning it again. 59 00:03:30,090 --> 00:03:31,060 There we go. 60 00:03:31,060 --> 00:03:36,120 And now, we get length of 2 for the value stored in the string variable. 61 00:03:36,120 --> 00:03:38,910 And we get a value of true for 62 00:03:38,910 --> 00:03:43,090 calling the even method on the value stored in the number variable. 63 00:03:43,090 --> 00:03:45,640 You can also chain method calls together. 64 00:03:45,640 --> 00:03:48,860 Let's suppose we wanna know whether a string contains an even number of 65 00:03:48,860 --> 00:03:49,930 characters. 66 00:03:49,930 --> 00:03:52,100 Well, we can call length on the string, and 67 00:03:52,100 --> 00:03:54,860 then call even on the number return from length. 68 00:03:54,860 --> 00:03:55,980 Let's try that. 69 00:03:55,980 --> 00:03:59,110 For our first attempt here, we're going to store everything in variables. 70 00:03:59,110 --> 00:04:06,042 So we'll say string = "AA", and then we'll create a variable named length, 71 00:04:07,871 --> 00:04:13,749 And store the value returned from calling a string.length in that. 72 00:04:13,749 --> 00:04:17,700 And then we can print the result of calling, 73 00:04:20,611 --> 00:04:24,630 The even method on the value that's stored in length. 74 00:04:24,630 --> 00:04:25,753 This is one way we can do it. 75 00:04:25,753 --> 00:04:30,208 Let's try running this, and we get tricked because there are two 76 00:04:30,208 --> 00:04:35,090 characters in the string "AA", and that is of course an even number. 77 00:04:35,090 --> 00:04:39,380 But there's a shorter way to do this, we can simply print out the result of 78 00:04:39,380 --> 00:04:44,888 calling, string.length, which will return a number. 79 00:04:44,888 --> 00:04:49,103 And then calling.even on that return value. 80 00:04:49,103 --> 00:04:51,930 And then we can get rid of all this other code. 81 00:04:51,930 --> 00:04:55,665 And if we try rerunning this, we'll get the exact same result, it's true. 82 00:04:55,665 --> 00:05:01,250 string.length returns the value 2, and 2 is an even number. 83 00:05:01,250 --> 00:05:04,550 You should only chain methods in situations where you're sure the first 84 00:05:04,550 --> 00:05:08,790 method call won't fail, otherwise, subsequent methods will return an error. 85 00:05:08,790 --> 00:05:11,110 But you'll often see this done in Ruby code, so 86 00:05:11,110 --> 00:05:12,590 it's good to be aware of the technique.