SHARE:
Uncategorized

Winning Arguments.

Flatiron School / 12 October 2013

The following is a guest post by Sonja Hall and originally appeared on her blog. Sonja is currently in the Ruby-003 class at The Flatiron School. You can follow her on Twitter here.

Parentheses are a subtle to include information in literature. They group numbers together in math equations, trail behind long organization titles in news reports, and even distinguish between an area code and a person’s unique telephone number.

When we switch gears into Ruby, parentheses are no longer representative of a polite nudge like they once were to us. Sitting alongside a method, they’re now considered powerful argumentation.

In a blogpost written by a Alan Skorkin, he says that contrary to popular belief, a method argument should be broken up into three separate, and easily digestible categories: required arguments, arguments with default values, and optional arguments.

image

Required arguments (aka: “Ordinal Params”)

Required arguments are your run-of-the-mill arguments. Nothing left to the imagination, require arguments are straightforward in their approach to gathering information from other sources.

image

Once you have defined your method, you will always need to provide the same number of arguments when calling the method.

image

image

Arguments with Default Values (aka: “Params with Default Values”)

Instead of assigning a specific number of arguments, you may also assign assigning a default value to an argument.

image

You can then call this method in one of two ways:

image

– OR –

image

The first example (above) calls the method without placing a value as its third parameter, resulting in the default value filled in as its third parameter. The second example has three parameters set and does not need to use the default value.

image

Optional arguments (aka: “Arguments as an Array”)

This is a special method argument that Ruby allows you to use that many refer to as “splat”.

To see an excellent blogpost describing splat’s usage in more depth, check out Manuel Neuhauser’s blog on the topic.

At a basic level, the optional argument allows you to decide at runtime how many arguments you’ll supply to a method:

image

Which could be called with:

image

You can then call it with any number of arguments, or no methods at all, resulting in an array with the exact number of arguments specified (or none at all if no arguments were defined).

image

Combinations

In addition to the three general uses of method arguments in Ruby, all of them can be mixed and matched together, creating a unique assortment of required, default values, and optional arguments.

For example:

image

If called with:

image

Since each required value gets an assigned value, the default value kicks in with its default, and the optional, or splat(*) argument would return an empty array.

While method arguments are seemingly basic at first glance, understanding the way they work by themselves, with default values and optional parameters, as well in unison while all hanging out in the same set of parentheses is an integral stepping stone to making your beginner Ruby methods function properly.

Happy arguing!

Pry - IRB's Smarter Younger Brother Previous Post Dot(.) Send Me to the Moon Next Post