Calling Methods on an Object5:13 with Jay McGavren
In this stage, we're going to learn how to call methods on an object. To be clear, we're not talking about passing an argument to a method. We mean taking a piece of data and calling a method that is only available on that piece of data.
When we talk about calling methods on an object, we mean taking a piece of data and calling a method that is only available on that piece of data. For example, strings have one set of methods available on them. And numbers have a different set of methods.
"a string".length "another string".upcase "a third string".reverse 7.even? -12.abs 5.next
When you call a method, it's important for Ruby to know which object you're calling it on, because the behavior of the method will vary based on the object. We type the dot operator following an object to indicate that we're calling a method on it.
puts "AA".length # 2 puts "AAAAAAAAA".length # 9 puts 3.odd? # true puts 4.odd? # false puts 3.even? # false puts 4.even? # true
Or, in place of the object itself, we can use a variable that refers to that object:
string = "AA" puts string.length # 2 number = 4 puts number.even? # true
- You want to know if a string contains an even number of characters.
lengthon the string
even?on number returned from
- Could store length in a variable, then call
even?on the value in the variable
- Instead, can "chain" the method calls by putting a dot operator and a method name directly following the previous method call
- Should only do this in situations where you're sure the first method call won't fail. Otherwise, subsequent methods will error. But you'll often see this done in Ruby code, so it's good to be aware of it.
puts "AAA".length.even? # false puts "AA".length.even? # true
[MUSIC] 0:00 [SOUND] In this stage, we're going to learn how to call methods on an object. 0:02 To be clear, we're not talking about passing an argument to a method. 0:08 We mean taking a piece of data and 0:12 calling a method that is only available on that piece of data. 0:14 For example, strings have one set of methods available on them, and 0:18 numbers have a different set of methods. 0:22 Almost every type of data available in Ruby has a large set of methods you 0:25 can call on it. 0:29 How large? 0:30 Any given string is going to have over 170 different methods. 0:32 A number is going to have over 130. 0:36 Don't worry, we're not going to try and teach you all of those methods right now. 0:39 You'll only use a small portion of them on a regular basis. 0:43 But it is important to understand how to call methods on an object, and 0:47 that's just what this stage will show you. 0:51 When you call a method, it's important for Ruby to know which object you're calling 0:54 it on, because the behavior of the method will vary based on the object. 0:58 We type the dot operator following an object to indicate that we're calling 1:02 a method on it. 1:06 So here you see a dot operator 1:07 that indicates that we're calling the length method on this string, "AA". 1:10 Here's another dot operator that indicates we're calling the length method on this 1:15 string instead. 1:19 And the result of the length method varies for these two different strings, 1:20 because obviously they have different lengths. 1:24 The first output's a length of 2, the second one output's a length of 9. 1:26 And here you see the data operator being used with several numbers. 1:30 So we call the odd method on the number 3, and 1:34 since 3 is odd and not even, that returns true down here. 1:38 By the way, question mark is a valid character at the end of a method name, 1:43 and so is exclamation point. 1:48 These two symbols are valid only at the very end of a method name. 1:49 This feature is unique to Ruby. 1:53 It's a little unusual for programming languages. 1:54 The question mark doesn't have any special meaning to the Ruby language itself but 1:57 by convention, a question mark usually means that a method is going to return 2:02 either true or false. 2:05 The same is true for this even method down here. 2:06 Here, we call the even method on the number 3. 2:09 And since 3 is odd and not even, 2:12 obviously that's going to return false down here at the output. 2:13 Likewise, if we do call dot even on the number 4, well, 2:18 4 is even so that is going to return true. 2:22 In place of the object itself, we can use a variable that refers to the object. 2:26 So if we wanted, instead of saying "AA".length, 2:30 we could sign the string "AA" to a variable named string, 2:35 and then we can print the length of the value that stored in the string variable. 2:40 So let's do that in place of put this "AA".length here, and 2:47 get rid of all this, and we can also do the same with a number. 2:51 Let's create a variable named, number. 2:56 We'll assign it number 4, and we'll call the even method 2:58 on the value that's stored in the number variable. 3:03 Save that, try running it. 3:09 Oops, I made a typo, one second. 3:13 You'll notice that it gave me an error down here. 3:17 Reporting an undefined method while obviously there's no method name to pute. 3:20 So we're gonna change that to puts, try rerunning it again. 3:25 There we go. 3:30 And now, we get length of 2 for the value stored in the string variable. 3:31 And we get a value of true for 3:36 calling the even method on the value stored in the number variable. 3:38 You can also chain method calls together. 3:43 Let's suppose we wanna know whether a string contains an even number of 3:45 characters. 3:48 Well, we can call length on the string, and 3:49 then call even on the number return from length. 3:52 Let's try that. 3:54 For our first attempt here, we're going to store everything in variables. 3:55 So we'll say string = "AA", and then we'll create a variable named length, 3:59 And store the value returned from calling a string.length in that. 4:07 And then we can print the result of calling, 4:13 The even method on the value that's stored in length. 4:20 This is one way we can do it. 4:24 Let's try running this, and we get tricked because there are two 4:25 characters in the string "AA", and that is of course an even number. 4:30 But there's a shorter way to do this, we can simply print out the result of 4:35 calling, string.length, which will return a number. 4:39 And then calling.even on that return value. 4:44 And then we can get rid of all this other code. 4:49 And if we try rerunning this, we'll get the exact same result, it's true. 4:51 string.length returns the value 2, and 2 is an even number. 4:55 You should only chain methods in situations where you're sure the first 5:01 method call won't fail, otherwise, subsequent methods will return an error. 5:04 But you'll often see this done in Ruby code, so 5:08 it's good to be aware of the technique. 5:11
You need to sign up for Treehouse in order to download course files.Sign up