你的問題沒有很好地解釋,但作爲一個快速猜測你想,我會建議看Array.group_by
什麼,這是可枚舉的一部分:
ary = [
%w[3.0.1.2 TOTAL Pass],
%w[3.0.1.3 TOTAL Pass],
%w[3.4.0.1 8K Fail],
%w[3.5.4.3 9K Fail],
%w[3.5.0.9 TOTAL Fail],
]
ary.group_by{ |a| a.last }
返回一個哈希,用鑰匙是最後一個元素的兩個不同值中的子陣列:
{
"Pass" => [
[0] [
[0] "3.0.1.2",
[1] "TOTAL",
[2] "Pass"
],
[1] [
[0] "3.0.1.3",
[1] "TOTAL",
[2] "Pass"
]
],
"Fail" => [
[0] [
[0] "3.4.0.1",
[1] "8K",
[2] "Fail"
],
[1] [
[0] "3.5.4.3",
[1] "9K",
[2] "Fail"
],
[2] [
[0] "3.5.0.9",
[1] "TOTAL",
[2] "Fail"
]
]
}
訪問的一個或另一個是容易的:
ary.group_by{ |a| a.last }['Pass']
[
[0] [
[0] "3.0.1.2",
[1] "TOTAL",
[2] "Pass"
],
[1] [
[0] "3.0.1.3",
[1] "TOTAL",
[2] "Pass"
]
]
或者:
ary.group_by{ |a| a.last }['Fail']
[
[0] [
[0] "3.4.0.1",
[1] "8K",
[2] "Fail"
],
[1] [
[0] "3.5.4.3",
[1] "9K",
[2] "Fail"
],
[2] [
[0] "3.5.0.9",
[1] "TOTAL",
[2] "Fail"
]
]
與['可枚舉#partition'(HTTP做在[一通(http://stackoverflow.com/a/15583917/1074296)://紅寶石-doc.org/core-2.0/Enumerable.html#method--partition) – dbenhur 2013-03-23 06:06:53