# Newbie: if / elseif

**URL:** <https://rubytalk.org/t/newbie-if-elseif/36170>\
**Category:** ruby-talk\
**Created:** [15 March 2007 00:00 UTC](https://rubytalk.org/t/newbie-if-elseif/36170 "2007-03-15T00:00:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![planetthoughtful](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@planetthoughtful](https://rubytalk.org/u/planetthoughtful)\
**Post date:** [15 March 2007 00:00 UTC](https://rubytalk.org/t/newbie-if-elseif/36170/1 "2007-03-15T00:00:10Z")

</div>

Hi All,

I'm a little confused about why the following two pieces of code seem  
to behave differently:

if $val =~ /this/i  
&nbsp;&nbsp;puts "this"  
elseif $val =~ /that/i  
&nbsp;&nbsp;puts "that"  
elseif $val =~ /the other/i  
&nbsp;&nbsp;puts "the other"  
end

if $val contains "this is the other value", for some reason "the  
other" is not put by the last elseif statement. However, the following  
code works as expected:

if $val =~ /this/i  
&nbsp;&nbsp;puts "this"  
end  
if $val =~ /that/i  
&nbsp;&nbsp;puts "that"  
end  
if $val =~ /the other/i  
&nbsp;&nbsp;puts "the other"  
end

Am I missing something about if / elseif in ruby?

Any help appreciated!

pt

---

<div class="post-metadata">

**Author:** ![planetthoughtful](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@planetthoughtful](https://rubytalk.org/u/planetthoughtful)\
**Post date:** [15 March 2007 00:05 UTC](https://rubytalk.org/t/newbie-if-elseif/36170/2 "2007-03-15T00:05:05Z")

</div>

Obviously I was missing something - the correct syntax. I'm now aware  
that the syntax should be "elsif" rather than "elseif".

Thanks all,

pt

> **···**
>
> On Mar 15, 9:59 am, "planetthoughtful" \<planetthought...@gmail.com\> wrote:
> 
> > Am I missing something about if / elseif in ruby?

---

<div class="post-metadata">

**Author:** ![Augie\_De\_Blieck\_Jr](https://avatars.discourse-cdn.com/v4/letter/a/2acd7d/32.png) [@Augie\_De\_Blieck\_Jr](https://rubytalk.org/u/Augie_De_Blieck_Jr)\
**Post date:** [15 March 2007 03:12 UTC](https://rubytalk.org/t/newbie-if-elseif/36170/3 "2007-03-15T03:12:17Z")

</div>

> if $val =~ /this/i  
> &nbsp;&nbsp;puts "this"  
> elseif $val =~ /that/i  
> &nbsp;&nbsp;puts "that"  
> elseif $val =~ /the other/i  
> &nbsp;&nbsp;puts "the other"  
> end

Just remember that even when you spell it right, it's going to drop  
out of the whole construct just as soon as it fulfills a RegEx. After  
it prints "this," it stops looking at any of the other "elsif"  
statements.

That short circuiting is a feature. =)

-Augie

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [16 March 2007 21:35 UTC](https://rubytalk.org/t/newbie-if-elseif/36170/4 "2007-03-16T21:35:08Z")

</div>

Thought I'd point out that Ruby's case statement is more powerful than  
many other languages' switch statements:

%w| pthisic thatch mother armadillo |.each{ |val|  
&nbsp;&nbsp;case val  
&nbsp;&nbsp;&nbsp;&nbsp;when /this/i  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "'#{val}' contains 'this'"  
&nbsp;&nbsp;&nbsp;&nbsp;when /that/i  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "'#{val}' contains 'that'"  
&nbsp;&nbsp;&nbsp;&nbsp;when /other/i  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "'#{val}' contains 'other'"  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "'#{val}' does not contain 'this', 'that', or 'other'"  
&nbsp;&nbsp;end  
}

#=\> 'pthisic' contains 'this'  
#=\> 'thatch' contains 'that'  
#=\> 'mother' contains 'other'  
#=\> 'armadillo' does not contain 'this', 'that', or 'other'

Also, note (as seen above) that the regexp you supplied as example  
match substrings even inside words. Just in case you wanted exact case-  
insenstive string matching, perhaps /\Athis\Z/i would be more  
appropriate. If you wanted exact word matching, perhaps /\bthis\b/i  
might be what you want.

> **···**
>
> On Mar 14, 5:59 pm, "planetthoughtful" \<planetthought...@gmail.com\> wrote:
> 
> > I'm a little confused about why the following two pieces of code seem  
> > to behave differently:
> > 
> > if $val =~ /this/i  
> > &nbsp;&nbsp;puts "this"  
> > elseif $val =~ /that/i  
> > &nbsp;&nbsp;puts "that"  
> > elseif $val =~ /the other/i  
> > &nbsp;&nbsp;puts "the other"  
> > end

---

<div class="post-metadata">

**Author:** ![7stud1](https://avatars.discourse-cdn.com/v4/letter/7/59ef9b/32.png) [@7stud1](https://rubytalk.org/u/7stud1)\
**Post date:** [15 March 2007 00:19 UTC](https://rubytalk.org/t/newbie-if-elseif/36170/5 "2007-03-15T00:19:50Z")

</div>

planetthoughtful wrote:

> **···**
>
> > On Mar 15, 9:59 am, "planetthoughtful" \<planetthought...@gmail.com\> \> wrote:
> > 
> > > Am I missing something about if / elseif in ruby?
> > 
> > Obviously I was missing something - the correct syntax. I'm now aware  
> > that the syntax should be "elsif" rather than "elseif".
> > 
> > Thanks all,
> > 
> > pt
> 
> You're not the only one. I wasn't quite so generous:
> 
> > **[Elseif v. elsif?](https://www.ruby-forum.com/t/elseif-v-elsif/87978)**
> >
> > 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...
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Chad\_Perrin1](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Chad\_Perrin1](https://rubytalk.org/u/Chad_Perrin1)\
**Post date:** [15 March 2007 03:20 UTC](https://rubytalk.org/t/newbie-if-elseif/36170/6 "2007-03-15T03:20:19Z")

</div>

Indeed -- that's the whole point of using elsif rather than just a list  
of separate if statements. The if/elsif/else construct is used for  
mutually exclusive flow control, where only one of several is to be  
executed. Using a series of if statements considers each in a vacuum,  
in order, while if/elsif/else considers them all as part of a greater  
whole.

> **···**
>
> On Thu, Mar 15, 2007 at 12:12:17PM +0900, Augie De Blieck Jr. wrote:
> 
> > \>if $val =~ /this/i  
> > \> puts "this"  
> > \>elseif $val =~ /that/i  
> > \> puts "that"  
> > \>elseif $val =~ /the other/i  
> > \> puts "the other"  
> > \>end
> > 
> > Just remember that even when you spell it right, it's going to drop  
> > out of the whole construct just as soon as it fulfills a RegEx. After  
> > it prints "this," it stops looking at any of the other "elsif"  
> > statements.
> > 
> > That short circuiting is a feature. =)
> 
> --  
> CCD CopyWrite Chad Perrin [[http://ccd.apotheon.org](http://ccd.apotheon.org) ]  
> "Real ugliness is not harsh-looking syntax, but having to  
> build programs out of the wrong concepts." - Paul Graham

---

<div class="post-metadata">

**Author:** ![planetthoughtful](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@planetthoughtful](https://rubytalk.org/u/planetthoughtful)\
**Post date:** [15 March 2007 04:05 UTC](https://rubytalk.org/t/newbie-if-elseif/36170/7 "2007-03-15T04:05:14Z")

</div>

Yes, that was the behaviour I was looking for. I noticed I provided an  
ambiguous example in that in another construct it would possibly have  
matched two different conditions. However, in my actual script I am  
looking for values that are mutually exclusive, so exiting on finding  
a value is fine.

All the best,

pt

> **···**
>
> On Mar 15, 1:12 pm, "Augie De Blieck Jr." \<augi...@gmail.com\> wrote:
> 
> > \> if $val =~ /this/i  
> > \> puts "this"  
> > \> elseif $val =~ /that/i  
> > \> puts "that"  
> > \> elseif $val =~ /the other/i  
> > \> puts "the other"  
> > \> end
> > 
> > Just remember that even when you spell it right, it's going to drop  
> > out of the whole construct just as soon as it fulfills a RegEx. After  
> > it prints "this," it stops looking at any of the other "elsif"  
> > statements.

---

<div class="post-metadata">

**Author:** ![James\_Britt](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@James\_Britt](https://rubytalk.org/u/James_Britt)\
**Post date:** [15 March 2007 05:41 UTC](https://rubytalk.org/t/newbie-if-elseif/36170/8 "2007-03-15T05:41:53Z")

</div>

So it doesn't work like the C variety?

if ( condition )  
&nbsp;&nbsp;statement // maybe done  
else if ( condition )  
&nbsp;&nbsp;statement // or maybe done  
else  
&nbsp;&nbsp;statement // done if the 1st and 2nd are not done

Honestly, my biggest difficulties so far are that I think of code somewhere between C and PHP (I like to call it a nicer C)

> **···**
>
> On Mar 15, 2007, at 12:20 PM, Chad Perrin wrote:
> 
> > On Thu, Mar 15, 2007 at 12:12:17PM +0900, Augie De Blieck Jr. wrote:
> > 
> > > > if $val =~ /this/i  
> > > > puts "this"  
> > > > elseif $val =~ /that/i  
> > > > puts "that"  
> > > > elseif $val =~ /the other/i  
> > > > puts "the other"  
> > > > end
> > > 
> > > Just remember that even when you spell it right, it's going to drop  
> > > out of the whole construct just as soon as it fulfills a RegEx. After  
> > > it prints "this," it stops looking at any of the other "elsif"  
> > > statements.
> > > 
> > > That short circuiting is a feature. =)
> > 
> > Indeed -- that's the whole point of using elsif rather than just a list  
> > of separate if statements. The if/elsif/else construct is used for  
> > mutually exclusive flow control, where only one of several is to be  
> > executed. Using a series of if statements considers each in a vacuum,  
> > in order, while if/elsif/else considers them all as part of a greater  
> > whole.
> > 
> > --  
> > CCD CopyWrite Chad Perrin [[http://ccd.apotheon.org](http://ccd.apotheon.org) ]  
> > "Real ugliness is not harsh-looking syntax, but having to  
> > build programs out of the wrong concepts." - Paul Graham

---

<div class="post-metadata">

**Author:** ![Chad\_Perrin1](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Chad\_Perrin1](https://rubytalk.org/u/Chad_Perrin1)\
**Post date:** [15 March 2007 08:19 UTC](https://rubytalk.org/t/newbie-if-elseif/36170/9 "2007-03-15T08:19:50Z")

</div>

Err . . . I'm pretty sure if, elsif, else works exactly like the C if,  
else if, else.

> **···**
>
> On Thu, Mar 15, 2007 at 02:41:53PM +0900, John Joyce wrote:
> 
> > So it doesn't work like the C variety?
> > 
> > if ( condition )  
> > &nbsp;&nbsp;statement // maybe done  
> > else if ( condition )  
> > &nbsp;&nbsp;statement // or maybe done  
> > else  
> > &nbsp;&nbsp;statement // done if the 1st and 2nd are not done
> 
> --  
> CCD CopyWrite Chad Perrin [[http://ccd.apotheon.org](http://ccd.apotheon.org) ]  
> "The first rule of magic is simple. Don't waste your time waving your  
> hands and hopping when a rock or a club will do." - McCloctnick the Lucid

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [16 March 2007 22:20 UTC](https://rubytalk.org/t/newbie-if-elseif/36170/10 "2007-03-16T22:20:42Z")

</div>

Hi --

> **···**
>
> On 3/15/07, Chad Perrin \<perrin@apotheon.com\> wrote:
> 
> > On Thu, Mar 15, 2007 at 02:41:53PM +0900, John Joyce wrote:  
> > \> So it doesn't work like the C variety?  
> > \>  
> > \> if ( condition )  
> > \> statement // maybe done  
> > \> else if ( condition )  
> > \> statement // or maybe done  
> > \> else  
> > \> statement // done if the 1st and 2nd are not done
> > 
> > Err . . . I'm pretty sure if, elsif, else works exactly like the C if,  
> > else if, else.
> 
> Pretty much, except in Ruby there's no need for a rule to resolve  
> if/else ambiguity, because 'end' always makes it clear.
> 
> David
> 
> --  
> Q. What is THE Ruby book for Rails developers?  
> A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> &nbsp;&nbsp;&nbsp;(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf\](http://www.rubypal.com/r4rrevs.pdf%5C))  
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
> A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Chad\_Perrin1](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Chad\_Perrin1](https://rubytalk.org/u/Chad_Perrin1)\
**Post date:** [16 March 2007 23:05 UTC](https://rubytalk.org/t/newbie-if-elseif/36170/11 "2007-03-16T23:05:46Z")

</div>

Thanks for adding that. I had forgotten about that, largely because I  
haven't written a line of C/C++ in about five years.

I imagine there are other differences as well, but for the general case  
I haven't run across any (yet). Still new to Ruby, may just not have  
encountered further differences yet.

> **···**
>
> On Sat, Mar 17, 2007 at 07:20:42AM +0900, David A. Black wrote:
> 
> > Hi --
> > 
> > On 3/15/07, Chad Perrin \<perrin@apotheon.com\> wrote:  
> > \>On Thu, Mar 15, 2007 at 02:41:53PM +0900, John Joyce wrote:  
> > \>\> So it doesn't work like the C variety?  
> > \>\>  
> > \>\> if ( condition )  
> > \>\> statement // maybe done  
> > \>\> else if ( condition )  
> > \>\> statement // or maybe done  
> > \>\> else  
> > \>\> statement // done if the 1st and 2nd are not done  
> > \>  
> > \>Err . . . I'm pretty sure if, elsif, else works exactly like the C if,  
> > \>else if, else.
> > 
> > Pretty much, except in Ruby there's no need for a rule to resolve  
> > if/else ambiguity, because 'end' always makes it clear.
> 
> --  
> CCD CopyWrite Chad Perrin [[http://ccd.apotheon.org](http://ccd.apotheon.org) ]  
> "Real ugliness is not harsh-looking syntax, but having to  
> build programs out of the wrong concepts." - Paul Graham
