我有一个包含以下代码的方法。
def save_question(content)
question = Question.new
question.content = content
question.save
end
当我在 if 语句中运行它时
if save_question(content)
puts "Everything is cool"
else
puts "Something went wrong"
end
该方法返回"Everything is cool"
。但是,如果我将方法更改为此
def save_question(content)
question = Question.new
question.content = content
return false unless question.save
end
然后 if 语句将返回"Something went wrong"
。我在这里错过了什么大事吗?我以为 save 方法返回 true,确实如此,但为什么该方法返回 false?