开发者

ruby method : If statement inside an if statement

i am getting a lot of errors in my program about not having/having too many "end" markers in my program. i have tested this piece of code before and it works but i was just wondering if someone could tell me whether or not i have enough "end"'s in this if statement or too many. Thanks

def hop!(d)

if d=开发者_如何转开发= 0  
    if @current_location.addpoint(0,1) < @boundary1 
    puts "error"
    elsif if @current_location.addpoint(0,1) > @boundary2
    puts "error2"
    else
    @current_location= @current_location.addpoint(0,1) 
    puts "all good"
    end
    end
elsif d == 1
if @current_location.addpoint(0,-1) < @boundary1 
    puts "error"
    elsif if @current_location.addpoint(0,-1) > @boundary2
    puts "error2"
    else
    @current_location= @current_location.addpoint(0,-1) 
    puts "all good"
    end
    end
elsif d== 2
if @current_location.addpoint(1,0) <  @boundary1 
    puts "error"
    elsif if @current_location.addpoint(1,0) >  @boundary2
    puts "error2"
    else
    @current_location= @current_location.addpoint(1,0) 
    puts "all good"
    end
    end

else d= 3
if @current_location.addpoint(-1,0) <  @boundary1 
    puts "error"
    elsif if @current_location.addpoint(-1,0) >  @boundary2
    puts "error2"
    else
    @current_location= @current_location.addpoint(-1,0) 
    puts "all good"
    end
    end

end


With a text editor that supports auto-reindentation, your code looks like this:

if d== 0  
  if @current_location.addpoint(0,1) < @boundary1 
    puts "error"
  elsif if @current_location.addpoint(0,1) > @boundary2
    puts "error2"
  else
    @current_location= @current_location.addpoint(0,1) 
    puts "all good"
  end
end
elsif d == 1
  if @current_location.addpoint(0,-1) < @boundary1 
    puts "error"
  elsif if @current_location.addpoint(0,-1) > @boundary2
    puts "error2"
  else
    @current_location= @current_location.addpoint(0,-1) 
    puts "all good"
  end
end
elsif d== 2
  if @current_location.addpoint(1,0) <  @boundary1 
    puts "error"
  elsif if @current_location.addpoint(1,0) >  @boundary2
    puts "error2"
  else
    @current_location= @current_location.addpoint(1,0) 
    puts "all good"
  end
end
else d= 3
  if @current_location.addpoint(-1,0) <  @boundary1 
    puts "error"
  elsif if @current_location.addpoint(-1,0) >  @boundary2
    puts "error2"
  else
    @current_location= @current_location.addpoint(-1,0) 
    puts "all good"
  end
end

end

Apart from the incorrect number of ends it seems like you're struggling with your elsifs, I can't think of any situation where elsif if [...] is a good thing to write


It's clear if you indent the code properly. In the following the code is indented properly and the problems with the ends and elsif if are corrected:

def hop!(d)
  if d == 0  
    if @current_location.addpoint(0,1) < @boundary1 
      puts "error"
    elsif @current_location.addpoint(0,1) > @boundary2
      puts "error2"
    else
      @current_location = @current_location.addpoint(0,1) 
      puts "all good"
    end
  elsif d == 1
    if @current_location.addpoint(0,-1) < @boundary1 
      puts "error"
    elsif @current_location.addpoint(0,-1) > @boundary2
      puts "error2"
    else
      @current_location = @current_location.addpoint(0,-1) 
      puts "all good"
    end
  elsif d == 2
    if @current_location.addpoint(1,0) < @boundary1 
      puts "error"
    elsif @current_location.addpoint(1,0) > @boundary2
      puts "error2"
    else
      @current_location = @current_location.addpoint(1,0) 
      puts "all good"
    end
  elsif d == 3
    if @current_location.addpoint(-1,0) < @boundary1 
      puts "error"
    elsif @current_location.addpoint(-1,0) > @boundary2
      puts "error2"
    else
      @current_location = @current_location.addpoint(-1,0) 
      puts "all good"
    end
  end
end

Also, in this case using case would be more elegant:

def hop!(d)
  case d
  when 0
    if @current_location.addpoint(0,1) < @boundary1 
      puts "error"
    elsif @current_location.addpoint(0,1) > @boundary2
      puts "error2"
    else
      @current_location = @current_location.addpoint(0,1) 
      puts "all good"
    end
  when 1
    if @current_location.addpoint(0,-1) < @boundary1 
      puts "error"
    elsif @current_location.addpoint(0,-1) > @boundary2
      puts "error2"
    else
      @current_location = @current_location.addpoint(0,-1) 
      puts "all good"
    end
  when 2
    if @current_location.addpoint(1,0) < @boundary1 
      puts "error"
    elsif @current_location.addpoint(1,0) > @boundary2
      puts "error2"
    else
      @current_location = @current_location.addpoint(1,0) 
      puts "all good"
    end
  when 3
    if @current_location.addpoint(-1,0) < @boundary1 
      puts "error"
    elsif @current_location.addpoint(-1,0) > @boundary2
      puts "error2"
    else
      @current_location = @current_location.addpoint(-1,0) 
      puts "all good"
    end
  end
end


case d
when 1:
  puts "error" and return if @current_location.addpoint(0,1) < @boundary1
  puts "error2" and return if  @current_location.addpoint(0,-1) > @boundary2
  puts "all good"
  break
when 2:
  puts "error" and return if @current_location.addpoint(1,0) < @boundary1 
  puts "error2" and return if @current_location.addpoint(1,0) >  @boundary2
  puts "all good"
  break
when 3:
  ...

This should do the trick as well i think.


It's definitely better to rewrite with case

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜