开发者

Ruby zip/join method

Right now I am using the zip method to merge two parallel arrays; one contains the data I am using and the other contains the labels for that data.

outFile.puts failure_labels.zip(failure_percents).join("|")

This call g开发者_运维百科ives me this output:

Build/Compile Failure|26.67%|Unknown Failure|25.45%|User Manifest Failure|25.21
%|Incomplete Build|2.79%|Invalid Clientspec|18.06%|Coverity System Error|0.66%|
Do Not Use Error|0.36%|Version Failure|0.36%|Coverity Defects|0.17%|Flash Build
Error|0.17%|Space Insufficient|0.13%|Integrate Failure|0.04%

But what I am trying to do is only join the inner elements of the 2d array created by zip, something like this:

Build/Compile Failure 26.67%|Unknown Failure 25.45%|User Manifest Failure 25.21
%|Incomplete Build 2.79%|Invalid Clientspec 18.06%|Coverity System Error 0.66%|
Do Not Use Error 0.36%|Version Failure 0.36%|Coverity Defects 0.17%|Flash Build
Error 0.17%|Space Insufficient 0.13%|Integrate Failure 0.04%

Is there any way to do this with the native join? or do I need to create my own function?

Thanks


Someone might think of something more clever, but here's a quick shot at it:

> a = %w(1 2 3 4 5)
=> ["1", "2", "3", "4", "5"]
> b = %w(a b c d e)
=> ["a", "b", "c", "d", "e"]
> a.zip(b).map { |ab| ab.join(" ") }.join("|")
=> "1 a|2 b|3 c|4 d|5 e"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜