Join array elements depending on the element's index

来源:互联网 发布:数据新闻网 编辑:程序博客网 时间:2024/06/15 03:02

I have this array: [ 'here', 'are', 'some', '123', 'data' ]. I would like to join the elements before the'123' into a string and the elements after the '123' into another string to get these three strings:

arr.chunk{|e| (e =~ /\d{3}/) || false}.map{|a| a.last.join(" ")}# => ["here are some", "123", "data"]

0 0
原创粉丝点击