# If...elseif...else, broken; need help

**URL:** <https://www.ruby-forum.com/t/if-elseif-else-broken-need-help/166324>\
**Category:** Rails\
**Created:** [April 23, 2009, 9:42pm UTC](https://www.ruby-forum.com/t/if-elseif-else-broken-need-help/166324 "2009-04-23T21:42:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DrV](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e36b37/32.png) [@DrV](https://www.ruby-forum.com/u/DrV)\
**Post date:** [April 23, 2009, 9:42pm UTC](https://www.ruby-forum.com/t/if-elseif-else-broken-need-help/166324/1 "2009-04-23T21:42:33Z")

</div>

I’ve been working on a new project and created a custom method for one  
of the models. Nothing major.

I brought up the console in --sandbox mode (also have tried in  
standard console), and tried playing with it. To my surprise there  
was a problem with the if/elseif conditional. The elseif portion  
simply would not work. So I tried a couple commands as so to test the  
actual if/elseif integrity like so:

person = “Jim”  
if person == “Sally”  
puts “Hi Sally”  
elseif person == “Jim”  
puts “Hi Jim”  
end

This returns nil. If I put a final catch it will return that  
value. What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![DrV](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e36b37/32.png) [@DrV](https://www.ruby-forum.com/u/DrV)\
**Post date:** [April 23, 2009, 9:46pm UTC](https://www.ruby-forum.com/t/if-elseif-else-broken-need-help/166324/2 "2009-04-23T21:46:39Z")

</div>

On Apr 23, 8:07 pm, DrV [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if person == “Sally”  
> puts “Hi Sally”  
> elseif person == “Jim”  
> puts “Hi Jim”  
> end

I assume you’ve got elsif rather than elseif (which doesn’t exist ) or  
else if, which isn’t the same.

> This returns nil.

puts always returns nil.

Fred

---

<div class="post-metadata">

**Author:** ![DrV](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e36b37/32.png) [@DrV](https://www.ruby-forum.com/u/DrV)\
**Post date:** [April 25, 2009, 5:14pm UTC](https://www.ruby-forum.com/t/if-elseif-else-broken-need-help/166324/3 "2009-04-25T17:14:13Z")

</div>

For many the observation that every operation in Ruby returns  
something - the primary effect, and it may do something else (a side-  
effect) is rather strange. So for example, we want the side-effect  
“put the stuff out to the printer” when we invoke puts and usually  
don’t care about the returned value (nil).

It is pretty important to separate what is returned from what it does.  
Sometimes they are the same (almost!), however. So == does indeed  
compare two comparable values, and it returns the result of that  
comparison (true or false).

On Apr 23, 2:45 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![DrV](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e36b37/32.png) [@DrV](https://www.ruby-forum.com/u/DrV)\
**Post date:** [April 26, 2009, 1:42am UTC](https://www.ruby-forum.com/t/if-elseif-else-broken-need-help/166324/4 "2009-04-26T01:42:35Z")

</div>

I’m _assuming_ that you’re actually worried about the fact that your  
conditional isn’t outputting anything. Unless you’re post was a typo,  
you mean to use “elsif” instead of “elseif”…

---

<div class="post-metadata">

**Author:** ![DrV](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e36b37/32.png) [@DrV](https://www.ruby-forum.com/u/DrV)\
**Post date:** [April 28, 2009, 3:14am UTC](https://www.ruby-forum.com/t/if-elseif-else-broken-need-help/166324/5 "2009-04-28T03:14:59Z")

</div>

Perhaps using elsif instead of elseif will help. But your code makes  
no attempt to return a value, so the nil is expected.  
If you are planning on returning a value, do something like this:  
result = if person == “Somebody”  
“foo”  
elsif person == “Another”  
“else if returned”  
else  
“no match”  
end

---

<div class="post-metadata">

**Author:** ![DrV](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e36b37/32.png) [@DrV](https://www.ruby-forum.com/u/DrV)\
**Post date:** [April 26, 2009, 2:49am UTC](https://www.ruby-forum.com/t/if-elseif-else-broken-need-help/166324/6 "2009-04-26T02:49:25Z")

</div>

DrV wrote:

> I’ve been working on a new project and created a custom method for one  
> of the models. Nothing major.
> 
> I brought up the console in --sandbox mode (also have tried in  
> standard console), and tried playing with it. To my surprise there  
> was a problem with the if/elseif conditional. The elseif portion  
> simply would not work. So I tried a couple commands as so to test the  
> actual if/elseif integrity like so:
> 
> person = “Jim”  
> if person == “Sally”  
> puts “Hi Sally”  
> elseif person == “Jim”  
> puts “Hi Jim”  
> end
> 
> This returns nil. If I put a final catch it will return that  
> value. What am I doing wrong?

1. Your code produces an error in all versions of Ruby.

2. Never use irb like interfaces in any programming language, and you  
will avoid a lot of confusion and you will never lose your work and you  
won’t have to retype anything. Instead open up a test program and run  
your code from the command line.

3. What does

puts “hello world”

return for you?
