Changes

Jump to: navigation, search

Ruby Methods

6 bytes added, 14:09, 31 March 2009
no edit summary
== Passing a Variable Number of Arguments to a Method ==
In the previous section of this chapter we looked at specifying a fixed number of arguments accepted by a method. Sometimes we don't always know in advance how many arguments will be needed. Ruby addresses this by allowing a method to be declared with a variable number of arguments. This achieved by using ''*args'' when declaring the method. The arguments passed to the method are then placed in an array where they may be accessed in the body of the method (see the [[Understanding Ruby Arrays]] for details on using arrays):
<pre>
</pre>
The above example passes 10 and 20 through to the ''multiply'' method. The method multiplies these two value values and returns the result. The assignment method (=) assigns the result to the variable ''value'' which is then displayed using ''puts''.
It is important to note that a method can return one, and only one value or object. If you need to return multiple values, consider placing the results in an array and returning the array.

Navigation menu