I have an array like this:
myarray = ['value1','value2','value3']
And I'm looking for a one element array like this:
mynewarray = ['value1|value2|value3']
I know how to do that using each and concatening in a string, but I'm wondering if there is a oneliner and beautiful Ruby way of doing so...
You can use the Array#join method.
myarray.join('|')
Array#join doc:
Returns a string created by converting each element of the array to a string, separated by sep.
[ "a", "b", "c" ].join #=> "abc"
[ "a", "b", "c" ].join("-") #=> "a-b-c"
Howsabout...
mynewarray = [myarray.join('|')]
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