I want to create a subclass of Date.
A normal, healthy, young rubyist, unscarred by the idiosyncrasy of Date’s implementation would go about this in the following manner:
require 'date'
class MyDate < Date
def initialize(year, month, day)
@original_month = month
@original_day = day
# Christmas comes early!
super(year, 12, 25)
end
end
And proceed to use it in the most expected manner…
require 'my_date'
mdt = MyDate.new(2012, 1, 28)
puts mdt.to_s
… only to be double-crossed by the fact, that the Date::new method is actually an alias to Date::civil, which doesn’t ever call initialize. In this case, the last piece of code prints “2012-01-28” instead of the expected “2012-12-25”.
Dear Ruby-community, wtf is this?
Is there some very good reason for aliasing new, so that it ignores initialize, and as a result, any common sense and regard for the client’s programmer’s mental health?
You define
initialize, but you create the new instance withnew.newreturns a new instance of the class, not the result ofinitialize.You may do:
Remark:
@original_month and @original_day are not available in this solution. The following solution extends Date, so you can access the original month and day. For normal dates, the values will be nil.
But I would recommend:
or