【发布时间】:2020-10-27 07:03:07
【问题描述】:
当我运行 rubocop 时,我不断犯错:
bin/main:70:6: C: Style/MultipleComparison: 避免将一个变量与条件中的多个项目进行比较,使用 Array#include?而是。
我的代码:
if board1 == [1, 2, 3] || board1 == [4, 5, 6] || board1 == [7, 8, 9] ||
board1 == [3, 5, 7] || board1 == [1, 5, 9] || board1 == [1, 4, 7] ||
board1 == [2, 5, 8] || board1 == [3, 6, 9]
board1.each { |state| board_states[state - 1] = 'X' }
puts "#{player1} win"
break
end
我是 ruby 和一般编码的新手。如何按照 rubocop 的建议将上述代码重构为更简洁?
【问题讨论】:
-
这个问题在codereview.stackexchange.com比较合适
-
文档提供了示例:Style/MultipleComparison