Ben Giddings
1/23/2007 9:16:00 PM
On Jan 23, 2007, at 04:47, Lloyd Zusman wrote:
> Just a small point: you don't need an 'else' clause after an 'exit',
> since nothing could follow that statement. Hence, you can just do
> this
> if you want, and it will have the same effect:
>
> if uinput == "EXIT" then
> exit
> end
> # do something else
>
> I tend to do that with 'exit', 'return', 'raise', 'throw', 'next',
> 'redo', and 'break' statements, to avoid excessive nesting. But
> that's
> just a stylistic thing.
For that matter, you don't need "then" unless it's all on one line:
if uinput == "EXIT" then exit end
exit if uinput == "EXIT"
or
if uinput == "EXIT"
exit
end
For safety reasons, you could even do:
if "EXIT" == uinput
exit
end
this way if you accidentally use "=" instead of "==" you'll get an
error:
Ruby will give you a warning if it spots a "=" in a conditional, but
often you want an error instead.
Ben