0
這裏是我的代碼簡短的樣本:DRY - 多行代碼執行相同的更新功能
def update_records!
# Teams.
home_team_record = PoolRecord.for_recordable_and_user(event_home_team, user)
home_team_record.update_for!(self)
away_team_record = PoolRecord.for_recordable_and_user(event_away_team, user)
away_team_record.update_for!(self)
# Division(s).
home_team_div_record = PoolRecord.for_recordable_and_user(event_home_team_division, user)
home_team_div_record.update_for!(self)
# Create/update PoolRecord for away_team division if they're in a different division.
unless event_away_team_division == event_home_team_division
away_team_div_record = PoolRecord.for_recordable_and_user(event_away_team_division, user)
away_team_div_record.update_for!(self)
end
# User.
user_record = PoolRecord.for_recordable_and_user(user, user)
user_record.update_for!(self)
end
DRY'ing這段代碼實際上是相當簡單的,如果不是因爲需要的條件檢查away_team部門。我可以創建一個傳入的第一個參數的字符串數組,並傳送給對象#。但是,就像我說的,我需要在一種情況下檢查一個條件。你會如何推薦DRY'ing?
非常優雅。謝謝您的幫助。 – keruilin