# If behavior

**URL:** <https://www.ruby-forum.com/t/if-behavior/135227>\
**Category:** Ruby\
**Created:** [April 19, 2008, 1:57pm UTC](https://www.ruby-forum.com/t/if-behavior/135227 "2008-04-19T13:57:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![frea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a8b319/32.png) [@frea](https://www.ruby-forum.com/u/frea)\
**Post date:** [April 19, 2008, 1:57pm UTC](https://www.ruby-forum.com/t/if-behavior/135227/1 "2008-04-19T13:57:57Z")

</div>

Hi,  
i recently started to learn ruby as well as rails and came across a  
problem.  
such code:

```auto
if session[:login]
   10/0
else
   11/0
end if
```

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:** ![frea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a8b319/32.png) [@frea](https://www.ruby-forum.com/u/frea)\
**Post date:** [April 19, 2008, 2:12pm UTC](https://www.ruby-forum.com/t/if-behavior/135227/2 "2008-04-19T14:12:38Z")

</div>

On Saturday 19 April 2008, Qwe Q. wrote:

> [/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

---

<div class="post-metadata">

**Author:** ![frea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a8b319/32.png) [@frea](https://www.ruby-forum.com/u/frea)\
**Post date:** [April 19, 2008, 2:13pm UTC](https://www.ruby-forum.com/t/if-behavior/135227/3 "2008-04-19T14:13:48Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Apr 19, 2008, at 1:57 PM, Qwe Q. wrote:

> ## [/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/).

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

# ==== (if session[:login] 10/0 else 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 G.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkgJ4UsACgkQJA/zY0IIRZZzfQCgl55ikkAE3ysW88WM0w1Qf5TD  
zrcAnR+hGkdRXkQrLFbUqXLeNofIJtiD  
=ROnh  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![frea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a8b319/32.png) [@frea](https://www.ruby-forum.com/u/frea)\
**Post date:** [April 19, 2008, 2:14pm UTC](https://www.ruby-forum.com/t/if-behavior/135227/4 "2008-04-19T14:14:13Z")

</div>

ehm great

```
if true then
  @users = User.find( :all )
else
  @users = User.find( :all )
end if
1
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @users }
end

```

works, but commenting out 1

```
if true then
  @users = User.find( :all )
else
  @users = User.find( :all )
end if
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @users }
end

```

makes the if not evaluated.

why?

---

<div class="post-metadata">

**Author:** ![frea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a8b319/32.png) [@frea](https://www.ruby-forum.com/u/frea)\
**Post date:** [April 19, 2008, 2:18pm UTC](https://www.ruby-forum.com/t/if-behavior/135227/5 "2008-04-19T14:18:29Z")

</div>

Hi,

On Sat, Apr 19, 2008 at 10:14 PM, Qwe Q. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if true then  
> @users = User.find( :all )  
> else  
> @users = User.find( :all )  
> end if  
> 1  
> respond\_to do |format|  
> 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:** ![frea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a8b319/32.png) [@frea](https://www.ruby-forum.com/u/frea)\
**Post date:** [April 19, 2008, 3:36pm UTC](https://www.ruby-forum.com/t/if-behavior/135227/6 "2008-04-19T15:36:50Z")

</div>

Hi –

On Sat, 19 Apr 2008, Qwe Q. wrote:

> ```
> format.xml { render :xml => @users }
> format.html # index.html.erb
> format.xml { render :xml => @users }
> 
> ```
> 
> 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 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

---

<div class="post-metadata">

**Author:** ![frea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a8b319/32.png) [@frea](https://www.ruby-forum.com/u/frea)\
**Post date:** [April 19, 2008, 2:20pm UTC](https://www.ruby-forum.com/t/if-behavior/135227/7 "2008-04-19T14:20:15Z")

</div>

On Saturday 19 April 2008, Florian G. wrote:

> As there is no second condition  
> on the second if, it is always false.

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

---

<div class="post-metadata">

**Author:** ![frea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a8b319/32.png) [@frea](https://www.ruby-forum.com/u/frea)\
**Post date:** [April 19, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/if-behavior/135227/8 "2008-04-19T21:49:14Z")

</div>

On Sat, Apr 19, 2008 at 5:14 AM, Qwe Q. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> format.xml { render :xml => @users }
> format.html # index.html.erb
> format.xml { render :xml => @users }
> end
> 
> ```
> 
> makes the if not evaluated.
> 
> why?

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