# Elseif v. elsif?

**URL:** <https://www.ruby-forum.com/t/elseif-v-elsif/87978>\
**Category:** Ruby\
**Created:** [March 7, 2007, 9:56am UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978 "2007-03-07T09:56:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 9:56am UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/1 "2007-03-07T09:56:54Z")

</div>

What the?? I just spent two days trying to figure out why I couldn’t  
reproduce the example in “Ruby in 20 minutes” and get it to work. After  
examining my code line for line against the example code and not being  
able to detect any error, I was assembling several code examples into a  
text file to post here, when I happened to notice ‘elsif’. Why  
didn’t Ruby flag ‘elseif’ as an error?

Does Ruby try differentiate itself in ridiculous ways like that just for  
the sake of being different? And why isn’t something like that  
explicitly pointed out in a beginning tutorial? So far, I have to give  
Ruby two thumbs down.

C++, Java, Javascript, php, Servlets+JSP programmer

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 10:00am UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/2 "2007-03-07T10:00:33Z")

</div>

On Wed, Mar 07, 2007 at 05:56:56PM +0900, 7stud 7stud wrote:

> Does Ruby try differentiate itself in ridiculous ways like that just for  
> the sake of being different? And why isn’t something like that  
> explicitly pointed out in a beginning tutorial? So far, I have to give  
> Ruby two thumbs down.

Ruby isn’t the only language that does that.

“Different” would be more like the way bash does it: “elif”

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 10:04am UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/3 "2007-03-07T10:04:34Z")

</div>

On 3/7/07, 7stud 7stud [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What the?? I just spent two days trying to figure out why I couldn’t  
> reproduce the example in “Ruby in 20 minutes” and get it to work. After  
> examining my code line for line against the example code and not being  
> able to detect any error, I was assembling several code examples into a  
> text file to post here, when I happened to notice ‘elsif’. Why  
> didn’t Ruby flag ‘elseif’ as an error?  
> Because it nvere sees it ☹

Look at two examples

if true then  
whatever  
elseif  
end

now elsif is seen as an undefined method but

if false then  
whatever  
elseif  
end

whatever and elseif are not evaluated.

I strongly advice you to use a syntax highlighting ediotr like e.g.  
vim, emacs, Jedit, geany and tons of others.

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 10:20am UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/4 "2007-03-07T10:20:36Z")

</div>

On Mar 7, 9:56 am, 7stud 7stud [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ruby two thumbs down.
> 
> C++, Java, Javascript, php, Servlets+JSP programmer
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Well, Ruby doesn’t try to differentiate itself in ridiculous ways just  
for the sake of being different. It’s not a person.  
However, it will spit out a “undefined method ‘elseif’ for main:Object  
(NoMethodError)” when you use ‘elseif’, so it’s really not a problem  
is it?

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 10:36am UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/5 "2007-03-07T10:36:43Z")

</div>

Alle mercoledì 7 marzo 2007, Robert D. ha scritto:

> if true then  
> whatever  
> elseif  
> end
> 
> now elsif is seen as an undefined method but

Not always. In Robert’s first example,

> if true then  
> whatever  
> elseif  
> end

you’ll get a NameError (undefined local variable or method `elseif’ for  
main:Object (NameError))

In the following example, instead, you get a syntax error:

if x \< 0 then puts “x\<0”  
elseif x \< 3 then puts “0\<=x\<3”  
else puts “x\>=3”  
end

The error message is:

syntax error, unexpected kTHEN, expecting kEND  
elseif x \< 3 then puts “0\<=x\<3”  
^  
Here, ruby doesn’t complain because elseif doesn’t exist, but because it  
finds  
a ‘then’ where it shouldn’t be (not following an if or elsif clause). By  
the  
way, being a syntax error (it when the interpreter is parsing the file,  
not  
when it executes it), this error is reported whatever the value of x is  
(and  
even if x doesn’t exist).

All these error messages aren’t very easy to understand for a novice. To  
make  
a comparison with other programming languages, I tried compiling a C  
program  
with a similar mistake (in this case writing ‘elseif’ instead of ‘else  
if’).  
The program was:

int main(){  
int a=3;  
int b=0;  
if( a==4){ b=1;}  
elseif(a==2){ b=2;} //should be else if  
else{ b=3;}}  
}

Compiling with gcc, the error message I got is:

test.c: In function ‘main’:  
test.c:5: error: expected ‘;’ before ‘{’ token

As you can see, the error message doesn’t speak of invalid keywords,  
but just  
of a missing ;

Stefano

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 10:59am UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/6 "2007-03-07T10:59:15Z")

</div>

> Maybe a chapter for that kind of pitfalls could be added somewhere -  
> well it probably is already, maybe somebody can indicate that.

Perhaps at the “Ruby from other languages” page :  
[http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/](http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)

I find this page very helpful.

Regards,

Chris

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 10:37am UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/7 "2007-03-07T10:37:46Z")

</div>

On 3/7/07, Hans S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > explicitly pointed out in a beginning tutorial? So far, I have to give  
> > (NoMethodError)" when you use ‘elseif’, so it’s really not a problem  
> > is it?
> 
> –  
> Hans

No of course it is not 🙂  
I think to understand the frustration of OP however.  
He is probably coming from a completely different world and it is not  
always easy to grasp new concepts.  
Therefore I preferred to ignore the aggressive nature of the post ;).  
He might even have a point when he says that this is maybe not really  
well documented, With this I do not mean the “elseif” of course but  
just the dynamic evaluation of the code.

Maybe a chapter for that kind of pitfalls could be added somewhere -  
well it probably is already, maybe somebody can indicate that.

This is however not a clearcut thing as it might seem at first view.

Robert

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 11:57am UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/8 "2007-03-07T11:57:51Z")

</div>

My output is:

~/2testing/dir1$ ruby rubyHelloWorld.rb  
Hello SallyJaneBob!

ruby version:

~/2testing/dir1$ ruby -v  
ruby 1.8.2 (2004-12-25) [universal-darwin8.0]

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 11:30am UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/9 "2007-03-07T11:30:53Z")

</div>

On 3/7/07, Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Good points Stefano, conclusion _always_ use “then” 🙂

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 12:02pm UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/10 "2007-03-07T12:02:00Z")

</div>

On Wed, Mar 07, 2007 at 07:54:15PM +0900, 7stud 7stud wrote:

> ```
> #functions:
> end
> 
> ```
> 
> end  
> if **FILE** == $0  
> mg = MegaGreeter.new([“Sally”, “Jane”, “Bob”])  
> mg.say\_hi  
> end

That’s because you’re not exercising the section under @names.nil?

Try: mg = MegaGreeter.new(nil)

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 11:54am UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/11 "2007-03-07T11:54:12Z")

</div>

Chris L. wrote:

> > Maybe a chapter for that kind of pitfalls could be added somewhere -  
> > well it probably is already, maybe somebody can indicate that.
> 
> Perhaps at the “Ruby from other languages” page :  
> [Ruby From Other Languages](http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)
> 
> I find this page very helpful.
> 
> Regards,
> 
> Chris

First, I’d like to say that the web site is really beautiful and eye  
catching. There are some minor problems, for instance, the code  
examples are in a small area width wise, so there is a horizontal scroll  
bar that you need to scroll to the right to see the latter portion of a  
line of code. However, the area with the code is very tall(more than  
one screen), and it is very inconvenient to page all the way down to the  
bottom in order to scroll to the right, and then go all the way back up  
in order to read the code. Also, no matter how wide I make my browser  
window(Safari 2.0.4), the area with the code does not expand  
horizontally. It should expand horizontally as the browser window gets  
wider, and the horizontal scroll bars should disappear.

If I run the following code, I don’t get any errors:

class MegaGreeter  
attr\_accessor :names

```
    #constructor
    def initialize(names = "world")
            @names = names
    end

    #functions:
    def say_hi
            if @names.nil?
                    puts "..."
            elseif @names.respond_to?("each")
                    @names.each do |name|
                            puts "Hello #{name}!"
                    end
            else
                    puts "Hello #{@names}!"
            end
    end

```

end  
if **FILE** == $0  
mg = MegaGreeter.new([“Sally”, “Jane”, “Bob”])  
mg.say\_hi  
end

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 12:06pm UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/12 "2007-03-07T12:06:01Z")

</div>

On 3/7/07, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > end
> > 
> > ```

You spoiled it 😉  
but this is a good way to explain it too…

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 12:05pm UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/13 "2007-03-07T12:05:01Z")

</div>

On 3/7/07, 7stud 7stud [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

I could explain why, but just follow Stefano’s advice and put “then”  
after each if elsif and elseif and you will see.

Robert

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 12:11pm UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/14 "2007-03-07T12:11:03Z")

</div>

On Wed, Mar 07, 2007 at 08:05:14PM +0900, Robert D. wrote:

> > That’s because you’re not exercising the section under @names.nil?
> > 
> > Try: mg = MegaGreeter.new(nil)
> 
> You spoiled it 😉  
> but this is a good way to explain it too…

and a good argument for code coverage testing (e.g. rcov)

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 12:08pm UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/15 "2007-03-07T12:08:32Z")

</div>

> Perhaps at the “Ruby from other languages” page :  
> [Ruby From Other Languages](http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)

I did a search on the C/C++ page for ‘elsif’ and it wasn’t found.

> Ruby isn’t the only language that does that.  
> “Different” would be more like the way bash does it: “elif”

To me ‘elif’ stands out like a red flag. ‘elsif’ is a more subtle  
differentiation, and I couldn’t spot it even though I had the problem  
narrowed down to 3 lines of code. The “Ruby in 20 Minutes” tutorial is  
obviously geared to the experienced programmer(beginner’s don’t know  
what classes are or what an ‘attr\_accessor’ is), so I would suggest  
putting this in the tutorial:

LOOK AT THE ELSIF SYNTAX CAREFULLY–IT’S ‘ELSIF’ NOT ‘ELSEIF’

with the ‘E’ in elseif in red.

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 12:24pm UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/16 "2007-03-07T12:24:55Z")

</div>

7stud 7stud wrote:

> > That’s because you’re not exercising the section under @names.nil?  
> > Try: mg = MegaGreeter.new(nil)
> 
> > You spoiled it 😉  
> > but this is a good way to explain it too…
> 
> What does that have to do with anything?

I tried it, and I got an error for the ‘elseif’. Why is that? If the  
first branch succeeds, why is the elsif branch even evaluated?

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 12:18pm UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/17 "2007-03-07T12:18:43Z")

</div>

> That’s because you’re not exercising the section under @names.nil?  
> Try: mg = MegaGreeter.new(nil)

> You spoiled it 😉  
> but this is a good way to explain it too…

What does that have to do with anything?

> Good points Stefano, conclusion _always_ use “then” 🙂

My first exposure to Ruby is the “Ruby in 20 Minutes” tutorial. If it’s  
good practice to always use ‘then’, how about putting it in the  
tutorial?

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 12:28pm UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/18 "2007-03-07T12:28:39Z")

</div>

7stud 7stud wrote:

> 7stud 7stud wrote:
> 
> > > That’s because you’re not exercising the section under @names.nil?  
> > > Try: mg = MegaGreeter.new(nil)
> > 
> > > You spoiled it 😉  
> > > but this is a good way to explain it too…
> > 
> > What does that have to do with anything?
> 
> I tried it, and I got an error for the ‘elseif’. Why is that? If the  
> first branch succeeds, why is the elsif branch even evaluated?

Hmm…I think I get it: Ruby doesn’t realize elseif is another branch,  
it just thinks its the next statement after the if statement. But as  
far as I know, all if statements are terminated with ‘end’.

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 12:32pm UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/19 "2007-03-07T12:32:07Z")

</div>

On Wed, Mar 07, 2007 at 08:28:43PM +0900, 7stud 7stud wrote:

> > I tried it, and I got an error for the ‘elseif’. Why is that? If the  
> > first branch succeeds, why is the elsif branch even evaluated?
> 
> Hmm…I think I get it: Ruby doesn’t realize elseif is another branch,  
> it just thinks its the next statement after the if statement. But as  
> far as I know, all if statements are terminated with ‘end’.

And yours is.

Add some spaces at the front of the ‘elseif’ line, so that it aligns  
with  
the ‘puts “…”’ on the line above. Then you’ll see how Ruby is  
interpreting  
your code.

Unlike python, Ruby doesn’t _force_ you to align your source in a  
particular  
way.

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 7, 2007, 12:37pm UTC](https://www.ruby-forum.com/t/elseif-v-elsif/87978/20 "2007-03-07T12:37:12Z")

</div>

Another question. The tutorial says:

> Save this file as â€œri20min.rbâ€, and run it as â€œruby ri20min.rbâ€.

and at the top of the file is the shebang:

#!/usr/bin/env ruby

If you run the program using ‘ruby filename’, do you need the shebang?  
I read something that said you only need the shebang if you want to  
execute programs using just the filename, e.g.:

$ HelloWorld.rb

also what does ‘env’ do? I read the man pages on the env function, and  
I can’t figure out what it does in the shebang.

[Next page](https://www.ruby-forum.com/t/elseif-v-elsif/87978.md?page=2)
