FAQ: Methods, Blocks, & Sorting - Splat!


#1

This community-built FAQ covers the “Splat!” exercise from the lesson “Methods, Blocks, & Sorting”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Splat!

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

Splat Arguments.
In the lesson we are taught that the placeholder text between the parenthesis on the method definition is called a parameter.
And the placeholder text between the parenthesis where the method is called is called an argument.

Then why would a parameter with a * next to it be called a ‘Splat Argument?’ Shouldn’t it be called a splat parameter?


#3

A parameter has a name that takes a positional argument. Splat is an argument array, by comparison. There can be any number of arguments.

We could call a method with an array as the argument, but we would either have it in tow already, or would have to construct it to pass it in to the method. Splat lets us write an argument list with variables pulled in from anywhere that don’t exist in a data structure.

The only thing that is not implied is order. This would need to be sorted out by using the splat in the call and set positional parameters in the method.

Bottom line, splat is not just a method tool, it is an array tool. There is a lot we can do with it so definitely do some reading up on this operator.

[*[1, 1, 2, 3, 5], *[8, 13, 21, 34, 55]]

=> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

#4

Thanks for the extra explanation on what splat is and how it is seen and accessed within a method.

However, I was hoping to get a clearer understanding of why it is called a Splat argument, as opposed to a splat parameter. Because it is always in the parameter of a method when it is used.


#5

Not so. It can also be in the method call.

def area(x, y)
  x * y
end

sides = [6, 7]
puts area(*sides)
# <- 42

#6

Brilliant. Thank you. I had no idea from the codecademy lesson that it was so useful. I’m going to session splat more tonight.