Ruby的惯用方式是有我返回在#survey_completed?
一个布尔值,这是我做的东西在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
这正是我一般需要寻找的 – Lee
,如果你的输入是字符串,请小心'!!',因为它会返回空白字符串的真值,这可能不是期望的行为。只是FYI –
@IuriG。在这种情况下,activesupport'Object#present?'很有用。 –