Using splat and optional arguments in Ruby methods
This section explains how to use the splat and optional argument types with Ruby methods to give flexible interfaces to programs. So far, we've covered traditional, named, and default arguments. However, Ruby isn't done yet. When it comes to passing data to methods, Ruby gives a few more key tools that we can use:
- Traditional splat arguments
- Keyword-based splat arguments
- Optional arguments
Traditional splat arguments
One of my favorite features in Ruby is how explicit many of its methods are, and splat may be one of my preferred arguments. The splat argument allows developers to pass an array of values into a method. Imagine that we are building a program that manages a baseball team. If we had a roster
method that printed out all of the player names, it would be messy to try to pass all of the names into the method one by one.
For example, this is what it would look like if we simply tried to pass three players into the method manually:
def roster...