在 Ruby 中从数组连接 2 x 2 元素的最优雅方法是什么?
例如: 我要加入数组:
ar = ['1', '2', '3', '4', '5']
得到
ar = [ '12', '34', '5' ]
感谢帮助
最佳答案
像往常一样,Enumerable是你的好 friend ,想让你的生活更轻松:
ar.each_slice(2).map(&:join)
唯一的技巧是使用 each_slice
的无 block 形式来获取一个枚举器,您可以将其map
以加入切片。
关于ruby-on-rails - 一次加入 2 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11996767/