我有分配對象的數組,我從一個數據庫調用創建:Array.count有塊不返回正確的答案
@assignments = @player.assignments
我想這個算來:
@assignments.count {|x| x.sets == 0.0}
這應該用0.0組計數賦值次數。但是,這總是返回@assignments中的對象總數。我已檢查過
@assignments.each {|x| puts x.sets == 0.0}
在所有情況下都不返回true。 任何線索?
編輯>
@assignments.map(&:sets)
=> [35.0, 120.0, 0.0, 0.0, 0.0, 0.0, 0.0, 12.0, 75.0, 0.0, 0.0, 0.0, 0.0]
使用'select'先'count'。 –
你可以顯示'@ assignments.map(&:sets)'的輸出嗎? –
它適用於我 [1] pry(main)>賦值= [35.0,120.0,0.0,0.0,0.0,0.0,0.0,12.0,75.0,0.0,0.0,0.0,0.0] => [35.0, 120.0,0.0,0.0,0.0,0.0,0.0,12.0,75.0,0.0,0.0,0.0,0.0] [2] pry(main)> assignments.count {| x | X == 0.0} => 9 [3]撬(主)> assignments.count => 13' –