Problems Extending DateTime

Howdy,

I’m writing my first Ruby application and have a few super-newbie
questions, so bear with me…I’m trying to add date precision to the
existing Date and DateTime classes.

My first attempt involves a module with a simple nested value class and
a standalone subclass of DateTime:

require ‘date’

module Runt
module DatePrecision

YEAR=0

HOUR_OF_DAY=3
…etc.

HOUR_OF_DAY_PREC = Precision.new(HOUR_OF_DAY)

def to_hour_of_day(arg)
#DateTimes are immutable
HOUR_OF_DAY_PREC.to_p(
DateTime,
*[arg.year,arg.mon,arg.day,arg.hour,0,0])
end

class Precision
include Comparable
attr :precision

def initialize(prec)
  @precision = prec 
end
    
def <=>(other)
  self.precision <=> other.precision
end

def to_p(klass, *args) 
  klass.new(*args)  
end                 

end
end
end

···


require ‘date’

class DateWithPrecision < DateTime

include Runt::DatePrecision

attr :precision

def initialize(*args)
@precision = args.pop
super(*args)
end

#This doesn’t work either?!
def initialize(yr=1,mon=1,day=1,hr=0,min=0,sec=0)
super(yr,mon,day,hr,min,sec)
end
end

However when I do something like this:

day_precise
= DateWithPrecision.new(1998,12,12,14,58,32,HOUR_OF_DAY_PREC)

or even this

day_precise = DateWithPrecision.new(1998,12,12,14,58,32)

I get errors like the following:

ArgumentError: wrong number of arguments(6 for 3)
dateprecisiontest.rb:116:in initialize' dateprecisiontest.rb:116:ininitialize’
/usr/lib/ruby/1.8/date.rb:1175:in new0' /usr/lib/ruby/1.8/date.rb:1175:innew’
dateprecisiontest.rb:56:in `test_to_precision’

4 tests, 16 assertions, 0 failures, 1 errors

I can’t seem to call Date/DateTime’s constructor from my subclass; even
when I don’t add or change arguments.

Is it, in fact, a bad idea to have a module create instance variables?
Is there another, simpler/better way of doing this?

I’m running Ruby 1.8.1 on RedHat 9.

Any feedback is greatly appreciated.

Thanks!

Matt