метод ruby: оператор if внутри оператора if - PullRequest
0 голосов
/ 05 октября 2011

В моей программе много ошибок, связанных с тем, что в моей программе слишком много маркеров «конца».Я уже тестировал этот кусок кода, и он работает, но мне просто интересно, может ли кто-нибудь сказать мне, достаточно ли у меня «конца» в этом операторе if или слишком много.Спасибо

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

Ответы [ 4 ]

3 голосов
/ 05 октября 2011

Понятно, если вы сделаете отступ в коде правильно.Ниже код корректно отступает, и проблемы с end s и elsif if исправляются:

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

Кроме того, в этом случае использование case будет более элегантным:

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
3 голосов
/ 05 октября 2011

С текстовым редактором, который поддерживает автоматическое переиндентирование, ваш код выглядит следующим образом:

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

Помимо неправильного числа end s, похоже, что вы боретесь с вашим elsifs, я не могу вспомнить ни одной ситуации, когда elsif if [...] - это хорошая вещь, чтобы написать

0 голосов
/ 17 ноября 2011

Определенно лучше переписать с case

0 голосов
/ 05 октября 2011
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:
  ...

Думаю, это должно сработать.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...