我有一个集合:
a = [[1, "a"], [nil, "b"], [nil, "c"], [2, "a"], [nil, "b"]] [[1, "a"], [nil, "b"], [nil, "c"], [2, "a"], [nil, "b"]]
我想得到:
[[1, "abc"], [2, "ab"]]
在Ruby中实现这一目标的正确方法是什么?如何有效地使用ruby内置集合函数?
a .slice_before{|k, _| k} .map{|a| [a.first.first, a.map(&:last).join]} # => [[1, "abc"], [2, "ab"]]
要么
a .slice_before{|k, _| k} .map{|a| a = a.dup; [a.first.shift, a.join]} # => [[1, "abc"], [2, "ab"]]