You define initialize , but you create a new instance with new . new returns a new instance of the class, not the result of initialize .
You can:
require 'date' class MyDate < Date def self.new(year, month, day) @original_month = month @original_day = day
Note: @original_month and @original_day are not available in this solution. The following solution extends the date, so you can access the original months and days. For normal dates, the values will be zero.
require 'date' class Date attr_accessor :original_month attr_accessor :original_day end class MyDate < Date def self.new(year, month, day)
But I would recommend:
require 'date' class MyDate < Date def self.create(year, month, day) @original_month = month @original_day = day
or
require 'date' class Date def this_year_christmas # Christmas comes early! self.class.new(year, 12, 28) end end mdt = Date.new(2012, 1, 28).this_year_christmas puts mdt.to_s
source share