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