Today I learned about the splat operator in Ruby.

Background

In my use case, I wanted to pass an array of fields into the .pluck operator for a query, but the pluck operator doesn’t take in arrays. Obviously, the solution here is to destructure the array. Were it JS I probably would have ju…


This content originally appeared on DEV Community and was authored by Patrick Wendo

Background

In my use case, I wanted to pass an array of fields into the .pluck operator for a query, but the pluck operator doesn't take in arrays. Obviously, the solution here is to destructure the array. Were it JS I probably would have just written .pluck(...pluck_fields) and moved on with my life. But funny enough, I have never had to destructure an array within function arguments. So I asked my supervisor and they told me to check the splat operator.

Splat Operator (* & **)

According to this article by Starr Horne, **They (splat operators) let you pass an array into a function expecting multiple arguments. The first item in the array becomes the first argument, the second item becomes the second argument and so on.*

Splat operator is more commonly used in function definition when the number of arguments is not known before hand, for instance:

def fun(*args)
    p args
end

But in my use case, I want to focus on the first definition I mentioned, they can be used to pass an array into a function expecting multiple arguments. This directly solves my problem, where now i can pass an array of fields I want plucked to the pluck function,

.pluck(*pluck_fields)

There is a lot more to explain about the splat operator and how it can also destructure Hashes as well, but that is beyond the scope of this post for now.

SPLAT!!!!


This content originally appeared on DEV Community and was authored by Patrick Wendo


Print Share Comment Cite Upload Translate Updates
APA

Patrick Wendo | Sciencx (2022-06-03T18:55:58+00:00) Today I learned about the splat operator in Ruby.. Retrieved from https://www.scien.cx/2022/06/03/today-i-learned-about-the-splat-operator-in-ruby/

MLA
" » Today I learned about the splat operator in Ruby.." Patrick Wendo | Sciencx - Friday June 3, 2022, https://www.scien.cx/2022/06/03/today-i-learned-about-the-splat-operator-in-ruby/
HARVARD
Patrick Wendo | Sciencx Friday June 3, 2022 » Today I learned about the splat operator in Ruby.., viewed ,<https://www.scien.cx/2022/06/03/today-i-learned-about-the-splat-operator-in-ruby/>
VANCOUVER
Patrick Wendo | Sciencx - » Today I learned about the splat operator in Ruby.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/03/today-i-learned-about-the-splat-operator-in-ruby/
CHICAGO
" » Today I learned about the splat operator in Ruby.." Patrick Wendo | Sciencx - Accessed . https://www.scien.cx/2022/06/03/today-i-learned-about-the-splat-operator-in-ruby/
IEEE
" » Today I learned about the splat operator in Ruby.." Patrick Wendo | Sciencx [Online]. Available: https://www.scien.cx/2022/06/03/today-i-learned-about-the-splat-operator-in-ruby/. [Accessed: ]
rf:citation
» Today I learned about the splat operator in Ruby. | Patrick Wendo | Sciencx | https://www.scien.cx/2022/06/03/today-i-learned-about-the-splat-operator-in-ruby/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.