I've got an array that at this point is ["firstname1 ", "lastname1", "firstname2 ", "lastname2", etc]
, and I'm trying to come up with a way to combine the strings such that I'll have an array of ["firstname1 lastname1", "firstname2 lastname2", etc]
.
Using Enumerable#each_slice
, you can iterate slice of n
elements (2 in your case).
By joining those two elements, you will get what you want.
a = ["firstname1 ", "lastname1", "firstname2 ", "lastname2"]
a.each_slice(2).map(&:join)
# => ["firstname1 lastname1", "firstname2 lastname2"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With