Stefano Crocco
4/19/2008 12:11:00 PM
On Saturday 19 April 2008, Qwe Qwe wrote:
> Hi,
> i recently started to learn ruby as well as rails and came across a
> problem.
> such code:
> [code]
> if session[:login]
> 10/0
> else
> 11/0
> end if
> [/code]
> doesn't inform me about division by zero. Actually it seems that none of
> paths is evaluated. I don't get it. I've checked few tutorials, none of
> the said anything about possibility of such behavior.
I think the problem is the if at the end of the last line. In ruby, the if-
then-else statement is closed by end, not end if. So, ruby thinks that the if
after the end is the beginning of a if modifier statement, that is, of
something like
puts "greater" if x > 5
Since the if modifier requires some condition after the if keyword, ruby will
look for it in the next line and interpret what you wrote like this:
(
if session[:login]
10 / 0
else
11 / 0
end
) if ... #whatever is in next line
This means that if the expression which gets interpreted as the condition for
the if modifier (that is, the second if) is false or nil, all the first if
statement won't be executed.
I hope this helps
Stefano