Forum: Ruby on Rails if...elseif...else, broken; need help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9858b5eb8d68da92bbac5bbf0bd831a8?d=identicon&s=25 DrV (Guest)
on 2009-04-23 21:42
(Received via mailing list)
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 <else> catch it will return that
value.  What am I doing wrong?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-04-23 21:46
(Received via mailing list)
On Apr 23, 8:07 pm, DrV <thevalent...@gmail.com> 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
Af8f5c2191526bdc38370c24d37a9f15?d=identicon&s=25 Chris Bird (Guest)
on 2009-04-25 17:14
(Received via mailing list)
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 Cheung <frederick.che...@gmail.com>
885ac43bca92fc2b1034356f3283e788?d=identicon&s=25 pharrington (Guest)
on 2009-04-26 01:42
(Received via mailing list)
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"...
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-26 02:49
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 <else> 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?
054ea2f04b5592b91f8223796cc53979?d=identicon&s=25 Brendon Whateley (brendon)
on 2009-04-28 03:14
(Received via mailing list)
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
This topic is locked and can not be replied to.