Languages like Ruby abstract away complexity. They allow a developer to think at the logical level and not so much on the syntax. Thereby Ruby’s ‘syntactical sugar’.
A Clever Tool
One of the features of Ruby’s sugar is the splat operator:
allows array values to be referenced as a comma separated list. All of the elements in an array can be passed to a method call as individual arguments versus passing in the array itself as a single argument.
# With splat (*)
arr = [1, 2, 3]
method_call(*arr)# Equivalent to:
method_call(arr, arr, arr)# A.K.A:
method_call(1, 2, 3)# As opposed to passing the whole array as a single argument:
When working with Ruby, you can call methods like
Array#maxto return the max value of an array.
max is available to
Math.It won't, however, return a max value of a single array argument passed (as a Ruby dev might like).
Ok, at least it’s there. What to do, then, to get the maximum value of an array?
Beginning to see why I'd like to splat something.
Math.maxthe elements of an array (i.e.
The syntax is…
Well, it’s that:
…. Let’s pass our array identifier to
var arr = [1, 2, 3];
var max = Math.max(…arr);
This will give us our max value from the array.
max is not a convenient method I can call on an array object.
Truth is, a developer has to get used to doing the same slightly different ways.
Am I ok with that?
Ehh… Guess I have to be :-)