A simple issue, but can’t figure out how to solve it, lacking knowledge in Ruby language:
class Game
def initialize
get_command
end
def get_command
command = gets
puts command # => POSITION
puts command != "POSITION" # => true
if command != "POSITION"
command = get_command
else
return true
end
end
end
a = Game.new
Whenever I run an application and type POSITION it always gets the true comparing to "POSITION" can anyone explain why?
Thanks
Because what you’re actually getting is “POSITION\n”. You can see this in irb:
You should strip the command before doing comparisons:
or
This will strip whitespace (including newlines) off of the input.