Using Ruby 2.4. I have an array of strings ...
["a", "b", "c"]
How do I take the above and convert each element into its own array of one element? So I would want the result of such an operation to be
[["a"], ["b"], ["c"]]
?
Using Ruby 2.4. I have an array of strings ...
["a", "b", "c"]
How do I take the above and convert each element into its own array of one element? So I would want the result of such an operation to be
[["a"], ["b"], ["c"]]
?
You can use zip
:
["a", "b", "c"].zip #=> [["a"], ["b"], ["c"]]
a.map { |s| Array(s) }
or
a.map { |s| [s] }
Also, you can use combination
or permutation
methods, it also provide little bit more functionality
a.combination(1).to_a
#=> [['a'], ['b'], ['c']]
a.combination(2).to_a
#=> [["a", "b"], ["a", "c"], ["b", "c"]]
a.permutation(1).to_a
#=> [['a'], ['b'], ['c']]
a.permutation(2).to_a
#=> [["a", "b"], ["a", "c"], ["b", "a"], ["b", "c"], ["c", "a"], ["c", "b"]]