我对这段代码感到困惑(来自 Chris Pine 的《学习编程》,第 8 章:编写您自己的方法)。
goodAnswer = false
while (not goodAnswer)
puts 'Do you like eating chimichangas?'
answer = gets.chomp.downcase
if (answer == 'yes' or answer == 'no')
goodAnswer = true
else
puts 'Please answer "yes" or "no".'
end
end
not
方法让我特别困惑。我认为这意味着“(while) variable(goodAnswer) is (not) false (i.e., true), continue this loop”。但恰恰相反:一旦变量 (goodAnswer
) 为真,循环就会中断。
所以我很好奇,not
函数到底做了什么?我无法在任何地方找到它的定义,这是我能找到的唯一使用示例。
最佳答案
大致意思是:
“虽然这不是一个好的答案”
(即,当 goodAnswer
为假时)。
关于ruby - "not"方法有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19164379/