2

I want to take a multidimensional array:

m_array = [["S39.91", "S39.91"], ["B99.9"]]

And convert it to individual strings based on its grouping:

String One = "S39.91, S39.91"
String Two = "B99.9"

1 Answer 1

5

What about:

m_array.map { |strings| strings.join(', ') }

That will give you an array:

["S39.91, S39.91", "B99.9"]

Which you can then use to assign to variables:

string_1, string_2 = ["S39.91, S39.91", "B99.9"]
Sign up to request clarification or add additional context in comments.

8 Comments

Great. Exactly what I was looking for.
Nice, but consider consolidating your code into one line, i.e. string1, string2 = m_array.map { |arr| arr.join(', ') }
@sagarpandya82 : No. First line works with any length, second line is for the special case of an array with two elements.
@EricDuminil I don't understand what you're are saying. The first line creates a new array, the second line assigns the variables. My suggestion is to do both in one line.
I'm just saying that we don't know if the array only has 2 elements, or more.
|

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.