# If behavior

**URL:** <https://rubytalk.org/t/if-behavior/45981>\
**Category:** ruby-talk\
**Created:** [19 April 2008 11:57 UTC](https://rubytalk.org/t/if-behavior/45981 "2008-04-19T11:57:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qwe\_Qwe](https://avatars.discourse-cdn.com/v4/letter/q/82dd89/32.png) [@Qwe\_Qwe](https://rubytalk.org/u/Qwe_Qwe)\
**Post date:** [19 April 2008 11:57 UTC](https://rubytalk.org/t/if-behavior/45981/1 "2008-04-19T11:57:58Z")

</div>

Hi,  
i recently started to learn ruby as well as rails and came across a  
problem.  
such code:  
[code]  
if session[:login]  
&nbsp;&nbsp;&nbsp;10/0  
else  
&nbsp;&nbsp;&nbsp;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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [19 April 2008 12:10 UTC](https://rubytalk.org/t/if-behavior/45981/2 "2008-04-19T12:10:56Z")

</div>

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]  
&nbsp;&nbsp;&nbsp;&nbsp;10 / 0  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;11 / 0  
&nbsp;&nbsp;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

> **···**
>
> 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]  
> > &nbsp;&nbsp;&nbsp;10/0  
> > else  
> > &nbsp;&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Florian\_Gilcher](https://avatars.discourse-cdn.com/v4/letter/f/57b2e6/32.png) [@Florian\_Gilcher](https://rubytalk.org/u/Florian_Gilcher)\
**Post date:** [19 April 2008 12:11 UTC](https://rubytalk.org/t/if-behavior/45981/3 "2008-04-19T12:11:06Z")

</div>

Yes, because the code never gets evaluated. if-blocks are ended by "end" and not "end if". What you wrote can be interpreted as:

> **···**
>
> On Apr 19, 2008, at 1:57 PM, 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]  
> > &nbsp;&nbsp;10/0  
> > else  
> > &nbsp;&nbsp;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.  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> # ==== (if session[:login] &nbsp;&nbsp;&nbsp;10/0 else &nbsp;&nbsp;&nbsp;11/0 end) if
> 
> So: execute the if statement, if.... As there is no second condition on the second if, it is always false. So the code inside the paranthesis will never get executed.
> 
> This is happening because ruby allows you to write a condition after a statement, e.g.:
> 
> # ==== do\_weird\_things if (true == ruby.is\_driving\_crazy?(you))
> 
> Regards,  
> Florian Gilcher

---

<div class="post-metadata">

**Author:** ![Qwe\_Qwe](https://avatars.discourse-cdn.com/v4/letter/q/82dd89/32.png) [@Qwe\_Qwe](https://rubytalk.org/u/Qwe_Qwe)\
**Post date:** [19 April 2008 12:14 UTC](https://rubytalk.org/t/if-behavior/45981/4 "2008-04-19T12:14:16Z")

</div>

ehm great

&nbsp;&nbsp;&nbsp;&nbsp;if true then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@users = User.find( :all )  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@users = User.find( :all )  
&nbsp;&nbsp;&nbsp;&nbsp;end if  
&nbsp;&nbsp;&nbsp;&nbsp;1  
&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # index.html.erb  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @users }  
&nbsp;&nbsp;&nbsp;&nbsp;end

works, but commenting out 1

&nbsp;&nbsp;&nbsp;&nbsp;if true then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@users = User.find( :all )  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@users = User.find( :all )  
&nbsp;&nbsp;&nbsp;&nbsp;end if  
&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # index.html.erb  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @users }  
&nbsp;&nbsp;&nbsp;&nbsp;end

makes the if not evaluated.

why?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Arlen\_Christian\_Mar1](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Arlen\_Christian\_Mar1](https://rubytalk.org/u/Arlen_Christian_Mar1)\
**Post date:** [19 April 2008 12:17 UTC](https://rubytalk.org/t/if-behavior/45981/5 "2008-04-19T12:17:41Z")

</div>

Hi,

> **···**
>
> On Sat, Apr 19, 2008 at 10:14 PM, Qwe Qwe \<frea@email.com\> wrote:
> 
> > &nbsp;&nbsp;&nbsp;if true then  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@users = User.find( :all )  
> > &nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@users = User.find( :all )  
> > &nbsp;&nbsp;&nbsp;end if  
> > &nbsp;&nbsp;&nbsp;1  
> > &nbsp;&nbsp;&nbsp;respond\_to do |format|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # index.html.erb  
> > ...
> 
> See Florian's message.  
> Replace "end if" with "end" only -- it's modifying the entire previous  
> conditional to only happen if the bit that follows returns true (or a truthy  
> value).
> 
> HTH,  
> Arlen

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [19 April 2008 12:18 UTC](https://rubytalk.org/t/if-behavior/45981/6 "2008-04-19T12:18:35Z")

</div>

I don't think so. Without a condition after the if, you'll get a SyntaxError.  
At least, that's what my tests show. Try running this:

ruby -e ' if true; puts "TRUE"; end if'

As I explained in my other post, since ruby doesn't find a condition on the  
same line of the if, it'll go on looking for it in the following line.

Stefano

> **···**
>
> On Saturday 19 April 2008, Florian Gilcher wrote:
> 
> > As there is no second condition  
> > on the second if, it is always false.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [19 April 2008 13:36 UTC](https://rubytalk.org/t/if-behavior/45981/7 "2008-04-19T13:36:16Z")

</div>

Hi --

> ehm great
> 
> &nbsp;&nbsp;&nbsp;if true then  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@users = User.find( :all )  
> &nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@users = User.find( :all )  
> &nbsp;&nbsp;&nbsp;end if  
> &nbsp;&nbsp;&nbsp;1  
> &nbsp;&nbsp;&nbsp;respond\_to do |format|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # index.html.erb  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @users }  
> &nbsp;&nbsp;&nbsp;end
> 
> works, but commenting out 1
> 
> &nbsp;&nbsp;&nbsp;if true then  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@users = User.find( :all )  
> &nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@users = User.find( :all )  
> &nbsp;&nbsp;&nbsp;end if  
> &nbsp;&nbsp;&nbsp;respond\_to do |format|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # index.html.erb  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @users }  
> &nbsp;&nbsp;&nbsp;end
> 
> makes the if not evaluated.
> 
> why?

$ ruby -e 'if true then p 1 else p 2 end if false'  
$ ruby -e 'if true then p 1 else p 2 end if true'  
1

The "if false" and "if true" at the end govern the whole thing. In  
your first example, you're doing:

if true then @users = ... else @users = ... end if 1

Since 1 is true, it gets executed.

In your second example, you're doing:

if true then @users = ... else @users = ... end if respond\_to...

I imagine respond\_to must return nil. Therefore, you're telling the  
first if statement not to execute.

This "if \<condition\> end if \<other\_condition\>" idiom is extremely  
unusual and almost certainly not what you intend to do. As others have  
said, it looks like you just want an if block, which ends with end.

David

> **···**
>
> On Sat, 19 Apr 2008, Qwe Qwe wrote:
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 9-12 Berlin  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS June 16-19 Berlin  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 24-27 London (Skills Matter)  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![Christopher\_Dicely](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@Christopher\_Dicely](https://rubytalk.org/u/Christopher_Dicely)\
**Post date:** [19 April 2008 19:48 UTC](https://rubytalk.org/t/if-behavior/45981/8 "2008-04-19T19:48:14Z")

</div>

see "If and unless modifiers", on this page:  
[http://www.rubycentral.com/pickaxe/tut\_expressions.html](http://www.rubycentral.com/pickaxe/tut_expressions.html)

> **···**
>
> On Sat, Apr 19, 2008 at 5:14 AM, Qwe Qwe \<frea@email.com\> wrote:
> 
> > ehm great
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;if true then  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@users = User.find( :all )  
> > &nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@users = User.find( :all )  
> > &nbsp;&nbsp;&nbsp;&nbsp;end if  
> > &nbsp;&nbsp;&nbsp;&nbsp;1  
> > &nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # index.html.erb  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @users }  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > works, but commenting out 1
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;if true then  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@users = User.find( :all )  
> > &nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@users = User.find( :all )  
> > &nbsp;&nbsp;&nbsp;&nbsp;end if  
> > &nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # index.html.erb  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @users }  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > makes the if not evaluated.
> > 
> > why?
