0
虛擬屬性我有以下功能工作得很好有多個參數
def holiday_hours_for(holiday)
hours["holiday_hours"][holiday.to_s] if hours["holiday_hours"][holiday.to_s]
end
我只是瞭解虛擬屬性,和時遇到一些麻煩搞清楚這個功能的二傳手版本。如何實現此功能...
def holiday_hours_for(holiday)=(hours)
self.hours["holiday_hours"][holiday.to_s] = hours if hours["holiday_hours"][holiday.to_s]
end
謝謝!
更新:我想出了以下內容,這是最好的方式嗎?
def update_holiday_hours_for(holiday, hours_text)
self.hours = Hash.new unless hours
self.hours["holiday_hours"] = Hash.new unless hours["holiday_hours"]
self.hours["holiday_hours"][holiday.to_s] = hours_text.to_s
end
假期將是一個指定哪個假期的參數。聖誕節,新年等 – Brandon
我不認爲你正在使用虛擬屬性。你只是設置一個散列值。虛擬屬性是指爲一個對象調用一個setter方法,但實際上它沒有由setter命名的屬性。當該方法被調用時,它設置一個實際屬性。 –
我明白了......謝謝! – Brandon