在 #survey_completed?
中,是否有更惯用的方法返回 bool 值?
这就是我在 C# 中做事的方式,我一直觉得最后一个返回 false
的三元子句是多余的,所以也许 Ruby 有更好的方法来做到这一点?
这是我的代码目前的样子:
def survey_completed?
self.survey_completed_at ? true: false
end
def survey_completed_at
# Seeing as the survey is submitted all or nothing, the time the last response is persisted
# is when the survey is completed
last_response = self.responses.last
if last_response
last_response.created_at
end
end
最佳答案
你可以使用双重否定:
def survey_completed?
!!survey_completed_at
end
关于Ruby 返回 bool 值的惯用方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18272034/