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"]]
perform_bulk and needed to get each id into its own array.a.map { |s| Array(s) }
or
a.map { |s| [s] }
["a", "b", "c"].map &Kernel.method(:Array)Kernel is mixed into everything so that we can pretend to have functions: ["a", "b", "c"].map &method(:Array)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"]]
["a", "b", "c"].each_cons(1).to_av#=> [["a"], ["b"], ["c"]]is yet another way.