0

I would like to split a string and append the split result to two separate arrays simultaneously. Is there a way to do this? For example:

mystrings = ['abc:def', 'ghi:jkl', 'mno:pqr']
first = []
second = []
mystrings.each do |string|
  first, second << string.split(':')
end

This doesn't work. But I didn't know if there was a syntactical Ruby way to perform the split and appending simultaneously.

0

1 Answer 1

4
mystrings = ['abc:def', 'ghi:jkl', 'mno:pqr']
first, second = mystrings.map{|str| str.split(":")}.transpose
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.