This line doesn't behave in the way I think you expect:

> while input.chomp.downcase != (EXIT || QUIT) #only works for exit

This says, "check if the downcased input is not equal to the value of
the expression `EXIT || QUIT`". What is the value of that expression?
In this case, it will resolve to EXIT, since the string "exit" is not
false or nil and is thus true. The value of QUIT is never evaluated.
So, if the input is not equal to exit, the while loop continues.

Why is that? In Ruby, all expressions have both a "value" and a
"truthiness". An expression is "falsy" if it evaluates to either
`false` or `nil`; otherwise it is `truthy`. In the case of an
expression like `foo || bar`, the truth table would look like this:

* foo is truthy, bar is truthy: result is `foo` and truthy
* foo is truthy, bar is falsy: result is `foo` and truthy
* foo is falsy, bar is truthy: result is `bar` and truthy
* foo is falsy, bar is falsy: result is `bar` and falsy

So you can see that in your case, foo is EXIT and bar is QUIT, both of
which are truthy values; thus the expression is "exit". To get what
you what want, try something like this:

    command = input.chomp.downcase
    while command != "exit" || command != "quit" ...

Or, more succinctly:

    case command
    when "exit", "quit"
      puts "exiting!"; ...
    else
      # do stuff
    end

~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/



On Sat, May 7, 2011 at 18:37, Bill W. <sirwillard42 / gmail.com> wrote:
> Hi everyone,
>
> This is my first post, so I hope I don't sound too inexperienced..
>
> I'm trying to teach myself Ruby, and have run into an issue with a while
> statement that will break if an input is "exit" or "quit".
> As of right now, it works if exit is input, but not quit
>
> I know I am completely misusing the entire thing, but here is what I
> came up with:
>
> EXIT = "exit" #need constants since Ruby gets pissed at string literals
> QUIT = "quit" #in a comparison
>
> print "Input: "
> input = gets
> while input.chomp.downcase != (EXIT || QUIT) #only works for exit
>
> #Do something
>
> print "Input: " #pick up the next input and check it
> input = gets
> end
>
> I know that Ruby has a lot of shortcuts, but if you post any please
> explain how they work (or provide a link to a good explanation.
>
> Thanks!
>
> --
> Posted via http://www.ruby-forum.com/.
>
>