The Seemingly Simple Splat Operator

So I just finished week 4 of the Flatiron School’s web development immersive program. I’m officially ⅓ of the way through and I just have to take a moment to reflect on how much I’ve learned in such a short amount of time.

When I started, I remember thinking a lab on nested hashes was tough and complex. Now I’m working with wonderful abstractions in Rails and trying to wrap my head around Partials, Locals and Connections. Since starting, my mind has been pushed, stretched and thrown past it’s comfort zone and into my keyboard but it’s all been worth it.

And that, in my humble opinion is a perfect segue into the more technical reflection of this past week.

The Splat Operator.

Splat.

I’ve become familiar with Ruby’s Splat Operator as something that expands the possibility of arguments getting passed into a method. But after just a little bit of research, this unassuming, seemingly simple asterisk has a lot more functionality than I realized.

While I’m pretty sure this is not the full extent of what the Splat Operator can do, I found the little tricks to be interesting and potential tools for the future.

First trick: If you have separate arguments in a method, adding the splat before an array will convert an array of elements into the arguments.

def add(int1, int2, int3)
puts int1 + int2 + int3
end
add(1, 2, 3)
#=>6
#this works
add([1, 2, 3])
#wrong number of arguments
#this does not
add(*[1, 2, 3])
#=>6
#this does! - The splat converts the array into arguments

Second trick: The Splat Operator can convert a string into an array. Wild!

foo = *'bar'
puts foo
# => ["bar"]
# splat operator converts string into an array

Third trick: The splat operator can convert a hash of hashes into an array of arrays. Honestly this was what impressed me the most about it. There is no need to iterate or dive into the nest to push keys and values back out as elements of arrays. It just does it on its own.

foo = *{ b: value1, a: value2, r: value3 }
puts foo
# => [[:b, value1], [:a, value2], [:r, value3]]
# Splat Operator makes hash of hashes an array of arrays

And while this probably only scratches the surface of what splat can do, (Double splat is another story), I’m beginning to ponder as I figure out these shortcuts if this is even right for me to use right now.

Sure the above splat code is quick and efficient… But if I were to go back into my code months from now, I might not fully grasp what’s happening with the abstraction.

I have to take into account how new I am to this shortcut and if I throw it in my code, I might not be able to figure out what it means later on. So for now, I’m alright with appreciating it for what it is, (awesome), but sticking to writing the clearest and most organized code I can. It’s what I’m striving for right after understanding all concepts thrown at me in this short amount of time.

Eh, that wasn’t so bad.

The daily reformation of my brain has and will continue to be a tough but rewarding journey. It’s one I know will ultimately yield amazing results because when I step back and reflect on the process, it already has.