Понятно, если вы сделаете отступ в коде правильно.Ниже код корректно отступает, и проблемы с 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