Ruby Reek错误重复条件
我有以下错误:Ruby Reek错误重复条件,ruby,reek,Ruby,Reek,我有以下错误: tests @action.placed.!=(true) at least 3 times (RepeatedConditional) 由以下代码生成: def left super unless @action.placed != true end def right super unless @action.placed != true end def move_forward
tests @action.placed.!=(true) at least 3 times (RepeatedConditional)
由以下代码生成:
def left
super unless @action.placed != true
end
def right
super unless @action.placed != true
end
def move_forward
super unless @action.placed != true
end
我们如何避免这种重复?别名有效吗
alias :right :left
alias :move_forward :left
我认为这是最好的解释。因为您的对象多次检查相同的条件,所以它可能承担了两个对象的角色,并且缺少一个抽象
解决方案可能是创建两个类,一个类的
@action.placed
始终为true,另一个类的@action.placed始终为true。另一个可能是将逻辑向上移动。或者将这些方法组合成1。理想情况下,目标是只需检查该条件一次。alias与他拥有的代码并不完全相同。虽然代码看起来很相似,但对super的调用调用的是另一个方法