Chaudhary Prakash Chaudhary Prakash - 5 months ago 8
Ruby Question

Convert group of value into single value whithin array

I have this kind of array:

a = ["2 3", "4 5 6"]


but I need to convert into this:

a = ["2", "3", "4", "5"]


How can I achieve this?

Answer

How about this?

a = ["2 3", "4 5 6"]
a.flat_map(&:split)
# => ["2", "3", "4", "5", "6"]

This is more-or-less equivalent to this:

a.map {|s| s.split }.flatten