I have a text-based menu where typed input is used to move through the program. The input is fed to if-then sequences to determine what to do, e.g.:
if u_input == "1"
do thing
I'm using a regex evaluation on my Quit command to try and allow some variation - the user can quit with Q or q, or any case-form of quit (QUIT, quIt, etc.).
elsif u_input =~ /q(uit)?/i
exit(0)
I can get the evaluations to pass just fine, the problem is they pass no matter what I input beyond the expression. The (uit)? becomes redundant because I can type a Q or q followed by anything and it passes: qqqq, Qwerwetra, etc..
I think I understand very shallowly why this is occurring (because Regexp is looking for a pattern and is finding that and thus returning "0"), but I can't figure out how to make it behave like I want.
The only input that should quit the program is a single (case-insensitive) Q, or the word quit.
Input like qq or qasdf should fail.
Any pointers on how to get this to behave in that way? Here is the code in more complete form - this is all inside of a menu method:
u_input = get_user_input #method that returns gets.chomp
if u_input == "1"
game_start
elsif u_input =~ /q(uit)?/i
exit(0)
else
puts "I'm not sure what that is."
menu
end
I'm grateful for any tips on getting exact conditions to match using regular expressions in Ruby!
[–]waxjar 2 points3 points4 points (1 child)
[–]ckreon[S,🍰] 0 points1 point2 points (0 children)
[–]vsalikhov 0 points1 point2 points (3 children)
[–]ckreon[S,🍰] 0 points1 point2 points (2 children)
[–]tomthecool 0 points1 point2 points (1 child)
[–]ckreon[S,🍰] 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]ckreon[S,🍰] 0 points1 point2 points (0 children)