Ruby methods are like functions in other programming languages. Ruby methods are used to group repeatable code into a single reusable methods.
Method names should not be capitalized. If you begin a method name with an uppercase letter, Ruby might think that it is a constant. The convention is to use underscores to separate words if your method name has more than one word.
Methods should be defined before calling them:
A methods definition starts with the 'def' keyword followed by the methods name.
Method parameters are specified between parentheses after the methods name.
The methods definition ends with the 'end' keyword on the last line similar to a closing bracket for a function.
def method_name(argument) puts argument end
Method names may end with a "!"(bang operator), a "?" or "=".
The bang methods(! at the end of method name) are called and executed just like any other method.
Methods that end with a question mark by convention return a boolean. They may not always return just true or false, but usually they return an object which in ruby is a truthy value.
Methods that end with an equals sign indicate an assignment method. For assignment methods the return value is ignored, the arguments are returned instead.
A Ruby method can accept arguments.
The list of arguments follows the method name.
The parentheses around the arguments are optional.
Multiple arguments are separated by a comma.
The arguments are positional.
def multiply_values(num1, num2) return num1 * num2 end
You can set default values of arguments. The default value does not need to be listed first, but arguments with defaults must be listed together.
def multiply_values(num1, num2 = 6) return num1 * num2 end