subclassing Date class

Discussion in 'Ruby' started by Josselin, Jun 29, 2006.

  1. Josselin

    Josselin Guest

    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
    Josselin, Jun 29, 2006
    #1
    1. Advertising

  2. 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
    Jan Friedrich, Jun 29, 2006
    #2
    1. Advertising

  3. Josselin

    S Wayne Guest

    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"
    S Wayne, Jun 30, 2006
    #3
  4. Josselin

    Guest

    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...
    , Jun 30, 2006
    #4
  5. Josselin

    Josselin Guest

    On 2006-06-30 14:17:23 +0200, ""
    <> 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 ?
    Josselin, Jun 30, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,193
    Michael Borgwardt
    May 30, 2004
  2. Mike Rooney
    Replies:
    1
    Views:
    276
  3. Sir Wilhelm the Sturdy

    Subclassing datetime.date

    Sir Wilhelm the Sturdy, Feb 6, 2010, in forum: Python
    Replies:
    2
    Views:
    228
    Gabriel Genellina
    Feb 7, 2010
  4. Replies:
    0
    Views:
    133
  5. nick

    Subclassing Date weirdness.

    nick, Apr 18, 2010, in forum: Javascript
    Replies:
    8
    Views:
    125
Loading...

Share This Page