Ruby Methods
Until this point in the material, every construct discussed would be evaluated immediately when the script is invoked. In Ruby, the Method object provides the ability to define a set of expressions which may be invoked at later point in the script, although not immediately.
Like a Variable, a Method has an identifier so that it can be referenced throughout the script by a developer defined name. This identifier has the same naming rules (and conventions) as variable names, with further options:
- A method name may end in question mark (?), exclamation point (!), or equal sign (=).
There are rules and conventions about when to use these extra characters in method names. The algorithmic exercises in this manual will not require expansion on the original ruleset.
A method definition begins with the keyword def, followed by the method identifier, a parenthesis contained list of parameters, a newline, method expressions, and finally a newline and keyword end. Method parameters are optional, and, technically speaking, a method expression set is also optional. Defining a method will not cause its expression set to run. To do so one must invoke the method.
METHODdef print_value
puts 'value'
end
A method is invoked by presence of its indicator in the script. At invocation, its expression set is executed. If there is a return value from the method, it may be captured in a variable or used as a conditional's predicate.
METHOD INVOCATIONdef print_value
puts 'value'
end
print_value
value
Parameters
Method definition may include a comma-separated list of parameters. These parameters become variables within the method expression set, and may be used as if variables had been defined in scope. If a method defines that it accepts parameters, parameter values must be provided at method invocation.
METHOD WITH PARAMETERSdef print_person(name, age)
puts "name: #{name}"
puts "age: #{age}"
end
As a bonus, this example introduces string interpolation, a way to output the value of a Ruby expression within a string. String interpolation requires the use of double-quotes (not single-quotes), and the Ruby expression must be encapsulated in an octothorpe-prepended set of curly braces, #.
Return Value
A method is useful for the ability to encapsulate related logic for organization and reuse. While it may be desirable to perform calculations in a method call, often the script which invokes the method will want to do something with the value the method's calculation determines. For this, methods have a return value, a value which is returned to the point in the script where the method was invoked. From the position in the script where the method is invoked, the method identifier may well be considered its return value.
In Ruby, methods have implicit return values -- whatever the last line of the method evaluates is returned to the calling script. This is useful shorthand, and the most common manner of returning values in the Ruby ecosystem.
METHOD RETURN VALUEdef biography(name, age)
"#{name} is #{age} years old."
end
puts biography('Lucy', 39)
Lucy is 39 years old.
In Ruby, there is also a way to be explicit about return using a return statement. A return statement is an expression which begins with the return keyword. A return value is given by whatever follows the return keyword within the return expression. If the return keyword is encountered anywhere during method execution, execution of the method stops, and the value of the return statement is returned to the invocation position.
METHOD WITH A RETURN STATEMENTdef biography(name, age, is_dog)
statement = "#{name} is #{age} years old."
if is_dog
return "#{statement} That's #{age * 7} in dog years!"
end
statement
end
puts biography('Fido', 7, true)
Fido is 7 years old. That's 49 in dog years!
Conclusion
The algorithmic exercises require you to define the solutions using method syntax. Doing so will allow the expression set which comprise the algorithm to be run with several test cases quickly. Such a use case is a great view into a benefit of organizing scripts into methods.