subclassing Date

Showing 1-10 of 10 messages
subclassing Date Michael Husmann 5/27/01 11:13 PM
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

subclassing Date Yukihiro Matsumoto 5/28/01 12:50 AM
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.

subclassing Date NAKAMURA, Hiroshi 5/28/01 12:50 AM
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

subclassing Date Michael Husmann 5/28/01 1:13 AM
"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

subclassing Date Michael Husmann 5/28/01 1:53 AM
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
 

subclassing Date Yukihiro Matsumoto 5/28/01 2:20 AM
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.

subclassing Date NAKAMURA, Hiroshi 5/28/01 2:20 AM
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.

subclassing Date Michael Husmann 5/28/01 2:33 AM
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

subclassing Date ts 5/28/01 3:20 AM
>>>>> "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

subclassing Date Yukihiro Matsumoto 5/28/01 11:20 AM
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.