0.1 More Objects and Methods

Looking up methods

Ruby objects are happy to tell you what methods they provide. You simply call the methods method on them.

Example Code:

Output Window

As you can see, you get a listing of all the methods on the number 1 that you could invoke. The names are prefixed with a colon (:) that you can safely ignore for now. If you find the results too muddled, you can easily sort them alphabetically. Try it for yourself - simply call the method sort on the result of methods:

Output Window

Invoking methods with arguments

When talking to an object via its methods, it is possible to give it additional information so it can give you an appropriate response.

This additional information is called the "arguments to a method." The name "argument" makes sense if you stop to think about the fact that methods are the paths of communication between objects.

Here's an example of an argument to the method index, which finds the position of the argument in the array:

Example Code:

Output Window

Here, index is the method and 'paper' the argument. If there is more than one argument, they can be passed to the method by simply separating them with commas.

Try using a method that takes two arguments - use the between? method to determine if the number 2 lies between the numbers 1 and 3.

Congratulations, guest!

% of the book completed


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