July 2011
Intermediate to advanced
400 pages
8h 46m
English
Ruby lets you specify default values for arguments. Default values can be assigned in the parameter list of a method using the usual assignment operator:
def aMethod( a=10, b=20 )
If an unassigned variable is passed to that method, the default value will be assigned to it. If an assigned variable is passed, however, the assigned value takes precedence over the default. Here I use the p() method to inspect and print the return values:
def aMethod( a=10, b=20 ) return a, b end p( aMethod ) #=> displays: [10, 20] p( aMethod( 1 )) #=> displays: [1, 20] p( aMethod( 1, 2 )) #=> displays: [1, 2]
In some cases, a method may need to be capable of receiving an uncertain number of arguments—say, for example, a method that processes ...
Read now
Unlock full access