6.1 Being Methodical

What are methods, really?

We've already talked about how objects in Ruby are analogous to objects in the real world. We've also briefly covered how they interact with one another through a construct called a "method." Since everything useful in a program happens through objects collaborating using methods, understanding them well is very important.

When one object calls a method on another, it is simply telling it what to do. A method, then, is simply programming jargon for something one object can do for another.

In the example below, we ask the object that represents the integer 1 to give us the next integer in the sequence. Do keep in mind that in the context of the program, "we" simply means the current object.

Example Code:

Output Window

So, to summarize, the data an object contains is what it is and its methods are what it can do. Implicit in this definition is the fact that the abilities of an object are limited to the methods it exposes.

Objectifying methods

Methods aren't exempt from Ruby's "everything is an object" rule. This means that the methods exposed by any object are themselves objects, and yes, you can use them as such.

All objects in Ruby expose the eponymous method method that can be used to get hold of any of its methods as an object.

Example Code:

Output Window

Here, we ask the object that is the integer 1 to give us the instance of the method next.

The method object still maintains a relationship with the object to which it belongs so you can still call it using the eponymous call method and it responds like a normal invocation of that method.

See for yourself.

Example Code:

Output Window

It's worth noting that in the normal course of things it is unlikely that you will need to - or should - look up a method object and use it as such.

Make it so

Let's write a method called reverse_sign that takes one object - an Integer - and changes a positive value to negative one, and vice-versa. We'll then dissect it, then move on to you practicing writing your own method.

Example Code:

Output Window

There, that works perfectly, converting 100 to -100 and -5 to 5 by simply subtracting the number given from 0.

Let's dissect this example a little, and you can then practice by writing your own.

First, note that we use the def keyword to create a method called reverse_sign on the current object. Since Ruby doesn't allow us to use spaces in method names, we replace them with underscores instead. It's also recommended that, as a convention, method names be in lower case.

The reverse_sign method accepts one parameter or argument. This is simply jargon for the objects a method needs from the caller in order for it to do its job. In this case, it's an integer. A method can accept any number of parameters (or none).

The return keyword specifies the object to be returned to the caller when the method has done its work. If no return keyword is specified, the object created by the last line in the method is automatically treated as the return value. A method must always return exactly one object.

Finally, the method is closed using the end keyword. There - simple, right?

Example Code:

Output Window

As you can see, even a method that does nothing at all and has no return produces an object - the nil. I'm printing out the class name because printing a nil returns an empty string, so you wouldn't see anything.

Be cautious when using return - calling return also exits the method at that point. No code in the method after the return statement is executed.

Example Code:

Output Window

This last example demonstrates two things:

  1. The return exits the method; the puts statement that comes right after is never run.
  2. Calling return without specifying an object to return results in a nil, which is returned by default.

An excellent practice is to either avoid using return entirely or always use return in the last line of the method (which is effectively the same thing). Having a method exit in the middle is always surprising, so being consistent in this manner makes for code that's easier to understand.

Beam me up, Scotty

Your turn. Write a method called add_two that adds 2 to any number passed to it and returns the result. Yes, please feel free to experiment using next in addition to the more obvious route of simply adding the integer 2 to the incoming number.

A more subtle lesson we learn from this exercise - especially if you got this to work using both addition as well as next - is that:

  1. As someone using a method, you don't usually care about how it works, just that it does.
  2. As the author of the method, you're free to change its internal implementation so long as it continues producing the same result. Switch from addition to next and back again - the master is happy either way.

Congratulations, guest!


% of the book completed

or

This lesson is Copyright © 2011-2014 by Sidu Ponnappa and Jasim A Basheer