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?'很有用。 –