[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

subclassing Date class

Josselin

6/29/2006 3:30:00 PM

Is it possible w Ruby to subclass the Date class in order to inherits
all Date methods and add my own methods and attributes ?

I need to pass an array of Date objects and I'd like to specify if this
date instance is a starting_date or ending_date...

joss

4 Answers

Jan Friedrich

6/29/2006 3:37:00 PM

0

Josselin wrote:
> Is it possible w Ruby to subclass the Date class in order to inherits
> all Date methods and add my own methods and attributes ?
Yes. Even it is possible to extend the Date class with your methods. :)

regards,
Jan

S Wayne

6/30/2006 6:27:00 AM

0

You can extend pretty much every class in Ruby, or you can add to the
base class itself directly, although this can be dangerous.

I'd recommend just creating your own Class that inherits from Date and
does what you need:

require 'date'

MyDate < Date
attr_accessor :date_type
end

m = MyDate.today
m.date_type='start_date'

m.to_s => "2006-06-30"

m.date_type => "start_date"

Dan Bernier

6/30/2006 12:17:00 PM

0


S Wayne wrote:
> I'd recommend just creating your own Class that inherits from Date and
> does what you need:

The only problem with that is you have to use the MyDate class
everywhere -- if there's a large codebase, that might be a problem. If
you add the methods to Date directly, that problem's solved. Might it
make sense to have a conversion function? I haven't gotten too deep
into this part of the dynamic language landscape yet...

Josselin

6/30/2006 9:25:00 PM

0

On 2006-06-30 14:17:23 +0200, "danbernier+ruby@gmail.com"
<danbernier@gmail.com> said:

> The only problem with that is you have to use the MyDate class
> everywhere -- if there's a large codebase, that might be a problem.
can you explain a little bit more ? i am a newrubies
> If
> you add the methods to Date directly, that problem's solved.
yes, but what about attribute (myDate_type ..)

I solved my problem with a two-dimensional array.. [[date] , [integer]]
but it's like using a hammer to smash a fly.... better add this
integer value in a date subclass ?