You're missing an end
keyword to if
. This is what it looks like if you indent the code properly:
def unsafe?(speed)
if speed < 40
return true
elsif speed > 60
return true
else
return false
end
def not_safe?(speed)
speed < 40 || speed > 60 ? true : false
end
end
As you can see the code will never get to def not_safe?(speed)
as the method has already returned. Ruby allows nested method definitions yet their actual use is universally discouraged.
This is what it should look like:
def unsafe?(speed)
if speed < 40
return true
elsif speed > 60
return true
else
return false
end
end
def not_safe?(speed)
speed < 40 || speed > 60 ? true : false
end
But this is really is a very overcomplicated way of doing something that can be handled with:
def unsafe?(speed)
!speed.between(40,60)
end
The whole idea of using the ternary operator is just plain strange as speed < 40 || speed > 60
evaluates to true or false anyways.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…