tags:

views:

61

answers:

2

Here is my code:

  case input
  when "quit" || "exit"
    break
  end

Only "quit" works here and not "exit".

How could I have "exit" work too without having to have a new "when" line?

+6  A: 
case input
when "quit", "exit"
  break
end
floatless
To explain why the original wasn't working: The `||` operator returns the first "truthy" operand. For example, the expression `1 || 2` returns 1, while `nil || 2` returns 2. So the expression `"quit" || "exit"` is equivalent to just `"quit"`.
Chuck
A: 

||operator evaluates the latter when the former is nil. "quit" is not nil. So "quit" || "exit" is "quit".

Shinya Miyazaki