我有以下代码(为了便于阅读而截断/释义)
def board_check?
@board.each {|row| check_row_for_truth_conditions(row)}
end
def check_row_for_truth_conditions(row)
return true if row.include("foo")
false
end
现在,每个迭代器的隐式返回始终是它迭代的集合。即;我得到了阵列,不是真或假。如果我不重构并执行类似下面的操作,它会按预期工作。但是我在很多地方都使用check_row_for_truth_conditions(并且它要长得多),所以想重构它
def board_check?
@board.each do |row|
return true if row.include("foo")
false
end
end
答案 0 :(得分:2)
一个选项是:
def board_check?
@board.any? {|row| row.include("foo") }
end