Forum: Ruby If-Then and "End"

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.
9d68b9f32999ecae3ea27fb8511df47e?d=identicon&s=25 Sam C. (cheesewhiz)
on 2007-01-23 05:32
I am making a program with various commands. One of them is "Exit" which
is supposed to exit the program. Here is my code:

if uinput = "EXIT" then
end
end

The problem is that the ruby compiler thinks that the first "end" is the
end to the If-Then statement, instead of the "end" that ends the
program. How do I work around this?
017e05d1a49ffa59ea03e149e7af720b?d=identicon&s=25 Chris Shea (Guest)
on 2007-01-23 05:40
(Received via mailing list)
Sam C. wrote:
>
> --
> Posted via http://www.ruby-forum.com/.

if uinput = "EXIT" then
exit
end

That should work. Each 'end' is matched with the closest previous
unmatched block opener ('if' in this case). It doesn't have anything to
do with terminating a script. hth
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 unknown (Guest)
on 2007-01-23 05:45
(Received via mailing list)
On Jan 22, 2007, at 11:32 PM, Sam C. wrote:

> end to the If-Then statement, instead of the "end" that ends the
> program. How do I work around this?

I think you are looking for 'exit' as the method to call to terminate
a program.
Also you aren't doing a comparison in your if condition, you are
doing an assignment.
So you want something like:

if uinput == "EXIT" then
   exit
else
   # do something else
end


Gary Wright
6be68ea08f17703ec184c0a861524c4a?d=identicon&s=25 Wang Dong (wangdong)
on 2007-01-23 05:55
(Received via mailing list)
"Sam C." <mathwhiz728@gmail.com>
дÈëÏûÏ¢ÐÂÎÅ:dde09efd974a47ac491498b540e43ed1@ruby-forum.com...
>
> --
> Posted via http://www.ruby-forum.com/.
>

It's not the Pascal£¬program end with an 'End.', just use exit.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-01-23 09:01
(Received via mailing list)
On 23.01.2007 05:45, gwtmp01@mac.com wrote:
>> The problem is that the ruby compiler thinks that the first "end" is the
>   exit
> else
>   # do something else
> end

Alternatively

exit if uinput == "EXIT"
...

  robert
56f2ce19706d05d18b5b66483aa13f98?d=identicon&s=25 Lloyd Zusman (Guest)
on 2007-01-23 10:48
(Received via mailing list)
gwtmp01@mac.com writes:

> you are doing an assignment.  So you want something like:
>
> if uinput == "EXIT" then
>   exit
> else
>   # do something else
> end

Just a small point: you don't need an 'else' clause after an 'exit',
since nothing could follow that statement.  Hence, you can just do this
if you want, and it will have the same effect:

  if uinput == "EXIT" then
    exit
  end
  # do something else

I tend to do that with 'exit', 'return', 'raise', 'throw', 'next',
'redo', and 'break' statements, to avoid excessive nesting.  But that's
just a stylistic thing.
46ec10622045f5671a1067865566f13c?d=identicon&s=25 Ben Giddings (Guest)
on 2007-01-23 22:16
(Received via mailing list)
On Jan 23, 2007, at 04:47, Lloyd Zusman wrote:
> I tend to do that with 'exit', 'return', 'raise', 'throw', 'next',
> 'redo', and 'break' statements, to avoid excessive nesting.  But
> that's
> just a stylistic thing.

For that matter, you don't need "then" unless it's all on one line:

if uinput == "EXIT" then exit end

exit if uinput == "EXIT"

or

if uinput == "EXIT"
   exit
end

For safety reasons, you could even do:

if "EXIT" == uinput
   exit
end

this way if you accidentally use "=" instead of "==" you'll get an
error:

Ruby will give you a warning if it spots a "=" in a conditional, but
often you want an error instead.

Ben
This topic is locked and can not be replied to.