2013-08-28 60 views
1

我收到以下错误在我的机器人类:Ruby的臭佬质量检查

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

你应该重构它在一个单一的方法

def robot_placed? 
    @robot.placed 
end 

,然后调用方法在你的方法

def right 
    @robot.right if robot_placed? 
end 

,并把robot_placed?在你班的私人部分;-)