I'm not sure if it makes sense to have too much logic in your initialize method - this means less flexibility in how you create your object. For example, I will have
class Calendar def self.new_using_strings(on_string, off_string, date_string) on = on_string.to_i # Or Integer(on_string) off = off_string.to_i # Or Integer(off_string) date = convert_to_date(date_string) new(on, off, date) end def self.convert_to_date(date_string) # Implementation goes here end def initialize(on, off, date) @on, @off, @date = on, off, date end end
Thus, if I want to create a calendar object using a date, not a date string, then I can do it.
I have not seen anyone else take this approach.
source share