两天前我才开始研究 Ruby 语言,并很快了解到我过于拘泥于 C 派生语言的思维方式......我正在尝试对字符串进行比较:
def menu_listen
action = gets
while !(action.eql?("up")) && !(action.eql?("down")) && !(action.eql?("close")) do
puts "'#{action}' is not a valid command at this time."
action = gets
end
return action
end
...之前是这样写的:
def main_listen
action = gets
while action != "up" && action != "down" && action != "close" do
puts "'#{action}' is not a valid command at this time."
action = gets
end
return action
end
我在这个网站上读到 thisString.eql?(thatString) 与 thisString == thatString 相同,因为两者都不起作用。我在命令提示符中输入的任何输入都不会通过 while 循环并给我这个作为响应:
'down
' is not a valid command at this time.
那么这是否意味着按下回车键也存储为命令提示符输入的新行?谁能告诉我如何实现这一点,以便字符串比较正常工作?