# If statement modifier, with else, warning

**URL:** <https://www.ruby-forum.com/t/if-statement-modifier-with-else-warning/179624>\
**Category:** Ruby\
**Created:** [November 30, 2009, 7:09pm UTC](https://www.ruby-forum.com/t/if-statement-modifier-with-else-warning/179624 "2009-11-30T19:09:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [November 30, 2009, 7:09pm UTC](https://www.ruby-forum.com/t/if-statement-modifier-with-else-warning/179624/1 "2009-11-30T19:09:18Z")

</div>

The following statement works fine, even though it generates a warning.  
With regard to “if” and “unless” being used as statement modifiers, a  
manual states: “Obviously, this syntax does not allow any kind of else  
clause.”

compare\_files unless @filename\_1 == nil or @filename\_2 == nil else  
@messagetextctrl.append\_text(" ==\> Please specify files to compare.  
\n")

Error Msg: “…rb:112: warning: else without rescue is useless”

Obviously, the statement can be changed to the normal “if-else-end”  
format, but that would not be as Ruby-ish as the above approach.

Is there a good reason why Ruby should not allow “else” to be used as in  
the example above?

Gary

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [November 30, 2009, 7:40pm UTC](https://www.ruby-forum.com/t/if-statement-modifier-with-else-warning/179624/2 "2009-11-30T19:40:03Z")

</div>

Gary H. wrote:

> The following statement works fine, even though it generates a warning.  
> With regard to “if” and “unless” being used as statement modifiers, a  
> manual states: “Obviously, this syntax does not allow any kind of else  
> clause.”
> 
> compare\_files unless @filename\_1 == nil or @filename\_2 == nil else  
> @messagetextctrl.append\_text(" ==\> Please specify files to compare.  
> \n")
> 
> Error Msg: “…rb:112: warning: else without rescue is useless”
> 
> Obviously, the statement can be changed to the normal “if-else-end”  
> format, but that would not be as Ruby-ish as the above approach.

What would be un-Rubyish about not using the statement modifier? The  
normal syntax would be far clearer here.

> Is there a good reason why Ruby should not allow “else” to be used as in  
> the example above?

Because it would lead to confusing code. Statement modifiers are fine  
for the simplest cases, but toss in any complexity and you’ve got chaos.

> Gary

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [November 30, 2009, 7:50pm UTC](https://www.ruby-forum.com/t/if-statement-modifier-with-else-warning/179624/3 "2009-11-30T19:50:41Z")

</div>

On 30.11.2009 19:09, Gary H. wrote:

> Obviously, the statement can be changed to the normal “if-else-end”  
> format, but that would not be as Ruby-ish as the above approach.
> 
> Is there a good reason why Ruby should not allow “else” to be used as in  
> the example above?

It’s a syntax error because of the ambiguity:

robert@fussel ~  
$ allruby -ce ‘puts 1 if 2 \> 0 else puts 3’  
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]  
-e:1: syntax error, unexpected kELSE, expecting $end  
puts 1 if 2 \> 0 else puts 3  
^  
ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-cygwin]  
-e:1: syntax error, unexpected keyword\_else, expecting $end  
puts 1 if 2 \> 0 else puts 3  
^

Btw, I believe you might have another issue in your code because of the  
precedence of “or”. It might be safer to use “||” or proper bracketing.

Kind regards

robert
