Ruby methods are similar to functions in other programming languages. They are used to encapsulate one or more repeatable statements into a single unit.
Naming Conventions
Method names should start with a lowercase letter. If a method name begins with an uppercase letter, Ruby may mistakenly interpret it as a constant, leading to potential parsing errors.
Simple Method Example
Here is an example of a method that prints "Hello World":
Methods with Parameters
Parameters are variables in a method definition that allow you to pass information (or arguments) into the method. They let the method perform actions based on the input values provided when the method is called. This is useful when the method needs to work with different inputs. Here's an example:
Methods with Multiple Parameters
You can also define methods with multiple parameters. Here's how:
In the above case, the add_numbers
method accepts two parameters, num1
and num2
, adds them together, and prints the result.
Default Parameters
Here, the greet method has a default value for the name parameter. If no argument is passed, it uses "Guest" as the default.
Positional & Named Parameters
Positional Parameters
In Ruby, positional parameters are passed to methods based on the order in which they are listed:
Here, "Alice" is assigned to first_name
, and "Smith" is assigned to last_name
based on their positions.
Named Parameters
Named parameters, also known as keyword arguments, are passed by specifying the parameter names along with their values:
With named parameters, the order doesn't matter because each argument is associated with a specific parameter name.
You can also set default values for named parameters:
This allows you to call the method without passing all arguments, and the default values will be used instead.