6.2 Calling a method

Cooperative objects

Thus far, we've only dealt with methods that only accept a single object as an argument or parameter. We'll expand this and talk about the ways in which methods can accept more than one parameter.

Let's take this slow and begin with two parameters.

Example Code:

Output Window

So, adding a second parameter is really simple - just add the new parameter separated from the original by a comma.

Example Code:

Output Window

Parameters can have default values too. Let's say we usually add three numbers, but occasionally just add two. We can default the last parameter in the previous example to 0 if nothing is passed to it.

Example Code:

Output Window

Older versions of Ruby - 1.8.x and older - required you to set default values for parameters starting with the last parameter in list and moving backward toward the first. The current version of Ruby (1.9.x) no longer has this limitation, but it's worth mentioning since Ruby 1.8.7 is still in use.

Ok, your turn. I shall simply ask that you make the tests pass for the exercise below.

Hint

Take a look at the failing test - it expects the method to use a default value when it doesn't pass in any name.

Output Window

Arraying your arguments

The list of parameters passed to an object is, in fact, available as a list. To do this, we use what is called the splat operator - which is just an asterisk (*).

The splat operator is used to handle methods which have a variable parameter list. Let's use it to create an add method that can handle any number of parameters.

We use the inject method to iterate over arguments, which is covered in the chapter on Collections. It isn't directly relevant to this lesson, but do look it up if it piques your interest.

Example Code:

Output Window

The splat operator works both ways - you can use it to convert arrays to parameter lists as easily as we just converted a parameter list to an array.

I'll show you how we can splat an array of three numbers into a parameter list so that it works with one of the examples from earlier in this lesson that accepts exactly three parameters.

Example Code:

Output Window

If you know some of the parameters to your method, you can even mix parameter lists and splatting. Again, older versions of Ruby (1.8.x or older) required you to place splatted parameters at the end of the parameter list, but this is no longer necessary.

In the example below, I'll expand on an earlier example to allow for the sum to be printed as a part of a message. We know what the message is - but we don't know how many numbers we'll need to add.

Example Code:

Output Window

Why don't you try it on for size? Create a method called introduction that accepts a person's age, gender and any number of names, then returns a String that introduces that person by combining all of these values to create a message acceptable to the tests.

As always, your objective is to make all the tests pass. Pay careful attention to the feedback from the tests, because even a simple, misplaced comma in the message could cause them to fail.

Hint

Accept age and gender as parameters and the names as a splatted array. You can concatenate names using the Array#join method.

Output Window

Naming parameters

This last section on method invocation is easiest demonstrated, then explained. Pay careful attention to the invocation of the add method in the example below. Look at how neatly we are able to pass configuration options to the method; the user of the add method gets to decide if the absolute value should be returned and if rounding should happen.

Example Code:

Output Window

Ruby makes this possible by allowing the last parameter in the parameter list to skip using curly braces if it's a hash, making for a much prettier method invocation. That's why we default the options to {} - because if it isn't passed, it should be an empty Hash.

As a consequence, the first invocation in the example has two parameters, the second, three and the last, seemingly five. In reality, the second and third invocations both have three parameters - two numbers and a hash.

A not-so-gentle workout

You are used to this by now. Write for me three methods - calculate, add and subtract. The tests should all pass. Take a look at the hint if you have trouble! And as a little extra hint: remember that you can use something.is_a?(Hash) or another_thing.is_a?(String) to check an object's type.

Hint

Write add and subtract first, then have calculate call one or the other depending on the options passed. Gotchas: The inject based approach for addition will require some modification for subtraction. For "calculate", you can't use both splatted arguments and last-parameter-is-a-hash at the same time through Ruby, so you'll have to work on the arguments inside of calculate. There's no neat way to do this - you have to check if the last argument to calculate is a Hash, then remove it from the list before calling add or subtract.

Output Window

Congratulations, guest!


% of the book completed

or

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