Ruby的Reek质量检查

时间:2013-08-28 16:10:51

标签: ruby reek

我在Robot类中遇到错误:

Commands tests @robot.placed at least 4 times (RepeatedConditional)

这是导致它的问题代码:

def move
  @robot.move_forward if @robot.placed
end

def left
  @robot.left if @robot.placed
end

def right
  @robot.right if @robot.placed
end

def report
  puts @robot.report_current_position if @robot.placed
end

我们如何重新组织这个以避免此警告?

1 个答案:

答案 0 :(得分:0)

你应该用一种方法重构它

def robot_placed?
  @robot.placed
end

然后调用方法中的方法

def right
  @robot.right if robot_placed?
end

然后放置robot_placed?在你班级的私人部分; - )