Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

subclassing Date

22 views
Skip to first unread message

Michael Husmann

unread,
May 28, 2001, 2:10:11 AM5/28/01
to
Using ruby 1.6.0 and trying to subclass the Date class like
this:

class My_date < Date
def initialize(st)
l = split(".")
y, m, d = l.collect{|s| s.to_i}
super(y,m,d)
end
end

ruby throws the following error messages when invoking:
d = My_date.new("2001.5.28")

/usr/local/lib/ruby/1.6/date.rb:33:in `civil_to_jd':
undefined method `-' for "2001.5.28":String (NameError)
/usr/local/lib/ruby/1.6/date.rb:139:in `exist3?'
/usr/local/lib/ruby/1.6/date.rb:147:in `new3'

I am calling the super method after converting the string
into 3 integers, but obviously the constructor of Date is
called before with the given string.

Any help would be appreciated.

Michael

Yukihiro Matsumoto

unread,
May 28, 2001, 3:23:47 AM5/28/01
to
Hi,

In message "[ruby-talk:15828] subclassing Date"


on 01/05/28, Michael Husmann <Michael...@teleatlas.com> writes:

|Using ruby 1.6.0 and trying to subclass the Date class like
|this:
|
|class My_date < Date
| def initialize(st)
| l = split(".")
| y, m, d = l.collect{|s| s.to_i}
| super(y,m,d)
| end
|end
|
|ruby throws the following error messages when invoking:
|d = My_date.new("2001.5.28")
|
|/usr/local/lib/ruby/1.6/date.rb:33:in `civil_to_jd':
|undefined method `-' for "2001.5.28":String (NameError)
|/usr/local/lib/ruby/1.6/date.rb:139:in `exist3?'
|/usr/local/lib/ruby/1.6/date.rb:147:in `new3'

You're calling `super("2001.5.28",...)'. I think there's something
wrong before calling super. Try:

class My_date < Date
def initialize(st)

l = st.split(".") # note "st."


y, m, d = l.collect{|s| s.to_i}
super(y,m,d)
end
end

matz.

NAKAMURA, Hiroshi

unread,
May 28, 2001, 3:46:09 AM5/28/01
to
Hi,

I'm sorry for replying matz' article. I've lost the
original post.

> |class My_date < Date
> | def initialize(st)
> | l = split(".")
> | y, m, d = l.collect{|s| s.to_i}
> | super(y,m,d)
> | end
> |end

Date.new is aliased to Date.new3 and Date.new0 is aliased
to Date.new in date.rb. Maybe following definition works
but it might not be intuitive...

class My_date < Date
def new3(st)
...
end
end

// NaHi

Michael Husmann

unread,
May 28, 2001, 4:07:33 AM5/28/01
to
"NAKAMURA, Hiroshi" wrote:

Hi NaHi,

sorry for ommitting the st variable in my first posting.

Thank you for your reply. I changed the code following your
proposal:

#!/usr/bin/env ruby -w

require "date"


class My_date < Date
def new3(st)

l = st.split(".")


y, m, d = l.collect{|s| s.to_i}

printf("year: %d, month: %d, day: %d\n", y, m, d)
super(y,m,d)
end
end

st = "2001.5.28"
d = My_date.new(st)

ruby still throws that error messages:


/usr/local/lib/ruby/1.6/date.rb:33:in `civil_to_jd':
undefined method `-' for "2001.5.28":String (NameError)

/usr/local/lib/ruby/1.6/date.rb:137:in `exist3?'
/usr/local/lib/ruby/1.6/date.rb:146:in `new3'
...

Is that a bug in ruby? Or a bug in that lib?
I don't think so, because I am absolutely new to ruby. So I
think there is a bug in my code, but I have no idea what's
wrong with it.

Could you look at it again? Thank you,

Michael

Michael Husmann

unread,
May 28, 2001, 4:47:35 AM5/28/01
to
Hi Matz,

yes I have forgotten the st before split. But the problem still exists after changing the code

Michael

--
Michael Husmann
p.A. Tele Atlas Deutschland GmbH, Am neuen Horizont 1, 31177 Harsum
PHONE: +(49) 5127 408-487, FAX: +(49) 5127 408-481
e-mail: Michael...@teleatlas.com
 

Yukihiro Matsumoto

unread,
May 28, 2001, 5:04:44 AM5/28/01
to
Hi,

In message "[ruby-talk:15840] Re: subclassing Date"


on 01/05/28, Michael Husmann <Michael...@teleatlas.com> writes:

|Could you look at it again? Thank you,

You're defining an instance method new3. I think you have to define a
class method, for example:

class My_date < Date
def My_date.new(st)


l = st.split(".")

p l


y, m, d = l.collect{|s| s.to_i}
printf("year: %d, month: %d, day: %d\n", y, m, d)
super(y,m,d)
end
end

st = "2001.5.28"
d = My_date.new(st)

It works fine for me.

matz.

NAKAMURA, Hiroshi

unread,
May 28, 2001, 5:03:21 AM5/28/01
to
Hi Michael,

I was wrong... I don't know how to do it.

> From: Michael Husmann
> Sent: Monday, May 28, 2001 5:40 PM

> > Date.new is aliased to Date.new3 and Date.new0 is aliased
> > to Date.new in date.rb.

I tested below and got "#<My_date: 2001.5.28,2299161>".

require 'date'

class My_date < Date
def new3(st)
l = st.split( "." )
y, m, d = l.collect { | s | s.to_i }

super( y, m, d )
end
end

p My_date.new0( "2001.5.28" )

I thought that it got worked but I know now it wasn't.

Hmm. Complex aliasing and new-initialize puzzle.
Michael: sorry I cannot help you.
Someone: enlighten me, too.

Michael Husmann

unread,
May 28, 2001, 5:30:46 AM5/28/01
to
Yukihiro Matsumoto wrote:

Hi Matz,

yes that works. Thank you for your help.
Using the -w option, ruby now warns:
(eval):4: warning: instance variable @__11185__ not initialized
(eval):4: warning: instance variable @__10729__ not initialized

I have switched -w off.

Regards,
Michael

ts

unread,
May 28, 2001, 5:56:12 AM5/28/01
to
>>>>> "N" == NAKAMURA, Hiroshi <na...@keynauts.com> writes:

N> p My_date.new0( "2001.5.28" )
[...]
N> Someone: enlighten me, too.

When new0 is defined as an alias, Date::new is not yet defined. This
mean that new0 is aliased on Class::new (rb_class_new_instance)

Class::new call #initialize which is Date#initialize

Try with

p My_date.new0( "2001.5.28" ).rjd


Guy Decoux

Yukihiro Matsumoto

unread,
May 28, 2001, 2:06:12 PM5/28/01
to
Hi,

In message "[ruby-talk:15856] Re: subclassing Date"


on 01/05/28, Michael Husmann <Michael...@teleatlas.com> writes:

|Using the -w option, ruby now warns:
|(eval):4: warning: instance variable @__11185__ not initialized
|(eval):4: warning: instance variable @__10729__ not initialized

This will be fixed in the near future (already fixed in the snapshot).

matz.

0 new messages