0

How do I combine arrays and convert them into string? I have two arrays:

a = ["09:00", "10:00", "11:00", "12:00"]
b = ["09:30", "10:30", "11:30", "12:30"]

How do I get the result string in this format?

c = '"09:00" - "09:30", "10:00" - "10:30", "11:00" - "11:30", "12:00" - "12:30"'
0

4 Answers 4

6

I'd do :

a = ["09:00", "10:00", "11:00", "12:00"]
b = ["09:30", "10:30", "11:30", "12:30"]
a.zip(b).map { |e1,e2| "\"#{e1}\" - \"#{e2}\"" }.join(', ')
Sign up to request clarification or add additional context in comments.

1 Comment

@Mischa Thanks,,,, Away from IRB.. So couldn't follow :)
2

This should work:

a.zip(b).map { |e1, e2| "\"#{e1}\" - \"#{e2}\"" }.join(', ')

Comments

1

How about:

a.zip(b).map { |e1, e2| "'#{e1}' - '#{e2}'" }.join(', ')

Comments

0

You could achieve this with:

a.each_with_index.map {|e,i| "#{e} - #{b[i]}"}

> a = ["09:00", "10:00", "11:00", "12:00"]
 => ["09:00", "10:00", "11:00", "12:00"]
> b = ["09:30", "10:30", "11:30", "12:30"]
 => ["09:30", "10:30", "11:30", "12:30"]
> a.each_with_index.map {|e,i| "#{e} - #{b[i]}"}.join(', ')
 => "\"09:00\" - \"09:30\", \"10:00\" - \"10:30\", \"11:00\" - \"11:30\", \"12:00\" - \"12:30\""

1 Comment

The final result should be a string, not an array ;-)

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.