Methods
Defining methods of an object is crucial since they exhibit an object's properties and behaviour.
Objects have methods
Methods are an object’s behaviour
Objects have methods, allowing us to do interesting stuff with them. An object’s methods are things that the object can do.
We could also ask them to make, and bring a cup of tea for us. Or we could ask them to remember a phone number or email password.
As Rubyists, we actually say that we “talk to objects”, or “send messages” to them: We do so by using (calling) methods that they respond to.
So, what can an object do?
That depends on their class (type). Numbers can do things that are useful for numbers, obviously. You can do the math, and ask them about their mathematical properties (e.g. “Are you an odd number?”). Strings (text) come with way more methods, and they’re often related to text transformations.
💡
Methods add behaviour that is useful to have for a particular type of object.
We’ve already used some methods in the previous chapters: E.g. "hello".upcase
calls the method upcase
on the String "hello"
.
Also, class
and is_a?
are methods defined on all objects in Ruby, and
therefore also defined on the String "a string"
: "a string".is_a?(String)
answers with true
.
Some methods, such as class
, is_a?
, are defined on all objects.
Let’s move on to see how we can use (call) these methods though.