# Elsunless: Compiles but does not run. Standard?

**URL:** <https://rubytalk.org/t/elsunless-compiles-but-does-not-run-standard/45120>\
**Category:** ruby-talk\
**Created:** [12 March 2008 14:19 UTC](https://rubytalk.org/t/elsunless-compiles-but-does-not-run-standard/45120 "2008-03-12T14:19:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fred\_Talpiot](https://avatars.discourse-cdn.com/v4/letter/f/7ea924/32.png) [@Fred\_Talpiot](https://rubytalk.org/u/Fred_Talpiot)\
**Post date:** [12 March 2008 14:19 UTC](https://rubytalk.org/t/elsunless-compiles-but-does-not-run-standard/45120/1 "2008-03-12T14:19:49Z")

</div>

In Ruby:  
1) 'if' can be followed by 'elsif'.  
2) 'unless' can NOT be followed by 'elsif'. It gets a syntax error.  
3) 'unless' can be followed by 'elsunless', but it does not run okay.  
3a) 'if' can also be followed by 'elsunless', with the same dismal  
results.  
4) So what's the standard?  
5) The following code:

> **···**
>
> -------------------  
> puts 'ruby version: ' + RUBY\_VERSION + ', ' + RUBY\_RELEASE\_DATE + ' for  
> ' + RUBY\_PLATFORM
> 
> unless true  
> &nbsp;&nbsp;puts "wrong wrong"  
> elsunless false  
> &nbsp;&nbsp;puts 'weird, but logical'  
> elsunless true  
> &nbsp;&nbsp;puts 'truly strange'  
> else  
> &nbsp;&nbsp;puts 'weirder yet' # This is what pops out!  
> end
> 
> # and 'elsif' is a syntax error following 'unless'  
> -------------------  
> Produces the following on WinXP:
> 
> > ruby unless.rb
> 
> ruby version: 1.8.6, 2007-09-24 for i386-mswin32  
> weirder yet
> 
> And, on Linux:
> 
> $ ruby unless.rb  
> ruby version: 1.8.6, 2007-09-24 for x86\_64-linux  
> weirder yet
> 
> We can be sure this behavior is not as intended. We cannot be sure  
> exactly what was intended.
> 
> The following rule is from the wiki reference page:  
> [http://en.wikibooks.org/wiki/Ruby\_Programming/Syntax/Control\_Structures#unless](http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Control_Structures#unless)
> 
> &nbsp;&nbsp;\>The unless expression is treated exactly like a negated if  
> expression:  
> &nbsp;&nbsp;\>  
> &nbsp;&nbsp;\> if !expression # is equal to using  
> &nbsp;&nbsp;\> unless expression
> 
> Obviously, that rule is not being followed. If it were, the 'elsif'  
> would  
> be permitted.
> 
> The interpreter has a bug. The documentation may also be wrong, hard to  
> know for sure...
> 
> name already taken  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Peter\_Hickman](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@Peter\_Hickman](https://rubytalk.org/u/Peter_Hickman)\
**Post date:** [12 March 2008 14:31 UTC](https://rubytalk.org/t/elsunless-compiles-but-does-not-run-standard/45120/2 "2008-03-12T14:31:08Z")

</div>

Are you sure about this. I am getting:

fred.rb:4:in `x': undefined method `elsunless' for main:Object (NoMethodError)

I think that there is no such thing as elsunless. How about some more realistic test code?

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada1](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@Nobuyoshi\_Nakada1](https://rubytalk.org/u/Nobuyoshi_Nakada1)\
**Post date:** [12 March 2008 14:32 UTC](https://rubytalk.org/t/elsunless-compiles-but-does-not-run-standard/45120/3 "2008-03-12T14:32:19Z")

</div>

Hi,

At Wed, 12 Mar 2008 23:19:49 +0900,  
Fred Talpiot wrote in [ruby-talk:294281]:

> 3) 'unless' can be followed by 'elsunless', but it does not run okay.  
> 3a) 'if' can also be followed by 'elsunless', with the same dismal  
> results.

No such keyword.

> unless true  
> &nbsp;&nbsp;puts "wrong wrong"  
> elsunless false

This is assumed a method call, but not reached.

> **···**
>
> --  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Stefan\_Lang1](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@Stefan\_Lang1](https://rubytalk.org/u/Stefan_Lang1)\
**Post date:** [12 March 2008 14:43 UTC](https://rubytalk.org/t/elsunless-compiles-but-does-not-run-standard/45120/4 "2008-03-12T14:43:07Z")

</div>

elsunless is not a keyword in Ruby. Let's reformat your  
code a little bit:

&nbsp;&nbsp;&nbsp;&nbsp;unless true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "wrong wrong"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsunless false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'weird, but logical'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsunless true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'truly strange'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'weirder yet' # This is what pops out!  
&nbsp;&nbsp;&nbsp;&nbsp;end

Syntax wise, Ruby sees the "elsunless" occurences here  
as plain method calls. Since the unless condition is true,  
the whole block gets never executed and there's no exception.

Let's compare in irb:

&nbsp;&nbsp;&nbsp;&nbsp;irb(main):001:0\> elsif true  
&nbsp;&nbsp;&nbsp;&nbsp;SyntaxError: compile error  
&nbsp;&nbsp;&nbsp;&nbsp;(irb):1: syntax error, unexpected kELSIF  
&nbsp;&nbsp;&nbsp;&nbsp;elsif true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1  
&nbsp;&nbsp;&nbsp;&nbsp;irb(main):002:0\> elsunless true  
&nbsp;&nbsp;&nbsp;&nbsp;NoMethodError: undefined method `elsunless' for main:Object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):2

Stefan

> **···**
>
> 2008/3/12, Fred Talpiot \<fredistic@gmail.com\>:
> 
> > [...]  
> > puts 'ruby version: ' + RUBY\_VERSION + ', ' + RUBY\_RELEASE\_DATE + ' for  
> > ' + RUBY\_PLATFORM
> > 
> > unless true  
> > &nbsp;&nbsp;puts "wrong wrong"  
> > elsunless false  
> > &nbsp;&nbsp;puts 'weird, but logical'  
> > elsunless true  
> > &nbsp;&nbsp;puts 'truly strange'  
> > else  
> > &nbsp;&nbsp;puts 'weirder yet' # This is what pops out!  
> > end

---

<div class="post-metadata">

**Author:** ![Thomas\_Wieczorek](https://avatars.discourse-cdn.com/v4/letter/t/e47c2d/32.png) [@Thomas\_Wieczorek](https://rubytalk.org/u/Thomas_Wieczorek)\
**Post date:** [12 March 2008 14:45 UTC](https://rubytalk.org/t/elsunless-compiles-but-does-not-run-standard/45120/5 "2008-03-12T14:45:28Z")

</div>

Ruby is an interpreted language, so it ignores your "unless true" part  
and doesn't parse what's after it until the "else". Your code should  
look like that:  
unless true  
&nbsp;&nbsp;&nbsp;&nbsp;puts "wrong wrong"  
&nbsp;&nbsp;&nbsp;&nbsp;elsunless false  
&nbsp;&nbsp;&nbsp;&nbsp;puts 'weird, but logical'  
&nbsp;&nbsp;&nbsp;&nbsp;elsunless true  
&nbsp;&nbsp;&nbsp;&nbsp;puts 'truly strange'  
else  
&nbsp;&nbsp;&nbsp;&nbsp;puts 'weirder yet' # This is what pops out!  
end

Nobu is right. Try "unless false" instead of "unless true" and you'll  
get a NameError

> **···**
>
> On Wed, Mar 12, 2008 at 3:19 PM, Fred Talpiot \<fredistic@gmail.com\> wrote:
> 
> > unless true  
> > &nbsp;&nbsp;puts "wrong wrong"  
> > elsunless false  
> > &nbsp;&nbsp;puts 'weird, but logical'  
> > elsunless true  
> > &nbsp;&nbsp;puts 'truly strange'  
> > else  
> > &nbsp;&nbsp;puts 'weirder yet' # This is what pops out!  
> > end

---

<div class="post-metadata">

**Author:** ![Stefan\_Lang1](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@Stefan\_Lang1](https://rubytalk.org/u/Stefan_Lang1)\
**Post date:** [12 March 2008 15:15 UTC](https://rubytalk.org/t/elsunless-compiles-but-does-not-run-standard/45120/6 "2008-03-12T15:15:07Z")

</div>

Damn, the "else" should not be indented.

Stefan

> **···**
>
> 2008/3/12, Stefan Lang \<perfectly.normal.hacker@gmail.com\>:
> 
> > elsunless is not a keyword in Ruby. Let's reformat your  
> > code a little bit:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;unless true  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "wrong wrong"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsunless false  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'weird, but logical'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsunless true  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'truly strange'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'weirder yet' # This is what pops out!  
> > &nbsp;&nbsp;&nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Fred\_Talpiot](https://avatars.discourse-cdn.com/v4/letter/f/7ea924/32.png) [@Fred\_Talpiot](https://rubytalk.org/u/Fred_Talpiot)\
**Post date:** [12 March 2008 16:27 UTC](https://rubytalk.org/t/elsunless-compiles-but-does-not-run-standard/45120/7 "2008-03-12T16:27:05Z")

</div>

Yup, you guys were right about ONE thing, the 'elsunless' was  
not being parsed. It ain't a keyword. It ain't nuthin'.

BUT the 'unless ... elsif' sequence SHOULD be accepted, if the  
wiki is right. The wiki claims that 'unless X' is the same as  
'if ! X'. And that is NOT true with regard to 'elsif'.

name already taken

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

---

<div class="post-metadata">

**Author:** ![Stefan\_Lang1](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@Stefan\_Lang1](https://rubytalk.org/u/Stefan_Lang1)\
**Post date:** [12 March 2008 16:55 UTC](https://rubytalk.org/t/elsunless-compiles-but-does-not-run-standard/45120/8 "2008-03-12T16:55:46Z")

</div>

The wiki first describes the plain if expression - without  
else or elsif. Then it describes unless and says it's the  
opposite of if. And only afterwards comes the description  
of else and elsif. Makes sense to me.

But it's a wiki and you're invited to improve it if you  
think it needs clarification.

(Well, and I'd find it ugly if Ruby actually allowed and  
elsif in an unless.)

Stefan

> **···**
>
> 2008/3/12, Fred Talpiot \<fredistic@gmail.com\>:
> 
> > Yup, you guys were right about ONE thing, the 'elsunless' was  
> > not being parsed. It ain't a keyword. It ain't nuthin'.
> > 
> > BUT the 'unless ... elsif' sequence SHOULD be accepted, if the  
> > wiki is right. The wiki claims that 'unless X' is the same as  
> > 'if ! X'. And that is NOT true with regard to 'elsif'.

---

<div class="post-metadata">

**Author:** ![Lionel\_Bouton](https://avatars.discourse-cdn.com/v4/letter/l/a88e4f/32.png) [@Lionel\_Bouton](https://rubytalk.org/u/Lionel_Bouton)\
**Post date:** [12 March 2008 17:15 UTC](https://rubytalk.org/t/elsunless-compiles-but-does-not-run-standard/45120/9 "2008-03-12T17:15:05Z")

</div>

Fred Talpiot wrote:

> Yup, you guys were right about ONE thing, the 'elsunless' was  
> not being parsed. It ain't a keyword. It ain't nuthin'.
> 
> BUT the 'unless ... elsif' sequence SHOULD be accepted, if the  
> wiki is right.

Why should we care about this wiki? There are thousands of ressources on Ruby. If you want to fix them when they aren't reliable enough, you are welcomed to do so but I don't see what you expect us to do with imaginary elsunless and elsif keywords in the unless constructs.

Lionel

---

<div class="post-metadata">

**Author:** ![Fred\_Talpiot](https://avatars.discourse-cdn.com/v4/letter/f/7ea924/32.png) [@Fred\_Talpiot](https://rubytalk.org/u/Fred_Talpiot)\
**Post date:** [13 March 2008 11:44 UTC](https://rubytalk.org/t/elsunless-compiles-but-does-not-run-standard/45120/10 "2008-03-13T11:44:46Z")

</div>

Lionel: I never meant you had to do anything, I was asking about the  
standard.

Why the Wiki?  
I went to [http://www.ruby-lang.org/en/documentation/](http://www.ruby-lang.org/en/documentation/) and started looking  
down the page for a 'language reference'. The wiki is the only one  
described on that page as a 'reference' to the language. The rest of  
the references there are to the libraries.

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