alias and subclasses

Discussion in 'Ruby' started by Tim Clark, Jun 25, 2007.

  1. Tim Clark

    Tim Clark Guest

    Hi everyone,

    The quick version:
    Is there a Ruby-ian way to get the alias method to propagate through
    subclasses?

    The longer one:
    I've got two classes with some simple inheritance:

    class Ticket
    def unreserve
    ...
    end
    alias unhold unreserve
    end

    class FamilyTicket < Ticket
    def unreserve
    ...
    end
    end

    FamilyTicket adds a bit of functionality to Ticket, and are reserved
    in different ways, so it's unreserve method overwrites its parent
    class. I would still like to use the aliased method name - e.g.
    ft = FamilyTicket.new
    ft.unhold

    However, the alias refers to Ticket#unreserve, not
    FamilyTicket#unreserve. From the docs I understand that this is by
    design, so my question is, is there a more Ruby-like way of doing the
    following?
    class Ticket
    def unreserve
    ...
    end

    def unhold
    unreserve
    end
    end

    Cheers,
    Tim Clark
     
    Tim Clark, Jun 25, 2007
    #1
    1. Advertising

  2. Tim Clark wrote:
    > Hi everyone,
    >
    > The quick version:
    > Is there a Ruby-ian way to get the alias method to propagate through
    > subclasses?
    >
    > The longer one:
    > I've got two classes with some simple inheritance:
    >
    > class Ticket
    > def unreserve
    > ...
    > end
    > alias unhold unreserve
    > end
    >
    > class FamilyTicket < Ticket
    > def unreserve
    > ...
    > end
    > end
    >
    > FamilyTicket adds a bit of functionality to Ticket, and are reserved in
    > different ways, so it's unreserve method overwrites its parent class. I
    > would still like to use the aliased method name - e.g.
    > ft = FamilyTicket.new
    > ft.unhold
    >
    > However, the alias refers to Ticket#unreserve, not
    > FamilyTicket#unreserve. From the docs I understand that this is by
    > design, so my question is, is there a more Ruby-like way of doing the
    > following?
    > class Ticket
    > def unreserve
    > ...
    > end
    >
    > def unhold
    > unreserve
    > end
    > end


    Alias it again?

    I talk about this topic (for monkey patching, but it would apply to
    subclassing as well) at http://djberg96.livejournal.com/97738.html if
    anyone is interested.

    Regards,

    Dan
     
    Daniel Berger, Jun 25, 2007
    #2
    1. Advertising

  3. On 6/25/07, Logan Capaldo <> wrote:

    > Of course now you have the problem of if the sublass decides to override
    > unhold instead of unreserve. The real solution to this problem IMO is to not
    > start aliasing things in the first place. A given method should have one
    > name. (I know Ruby does it with map/collect etc., but that doesn't mean I
    > have to agree does it? :) )


    Also, I always look at this as a feature and not a bug, as in:

    alias_method :eek:ld_something, :something

    def something
    #do some hackery
    old_something
    end
     
    Gregory Brown, Jun 25, 2007
    #3
    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. Marc Twain
    Replies:
    2
    Views:
    709
    Chris Uppal
    Jan 7, 2004
  2. Steve Nakhla
    Replies:
    0
    Views:
    636
    Steve Nakhla
    Sep 27, 2004
  3. John C. Bollinger

    Type parameters and anonymous subclasses

    John C. Bollinger, Feb 18, 2005, in forum: Java
    Replies:
    2
    Views:
    382
    John C. Bollinger
    Feb 21, 2005
  4. EdwardH

    Generics and subclasses

    EdwardH, Oct 29, 2005, in forum: Java
    Replies:
    6
    Views:
    499
    Chris Smith
    Nov 6, 2005
  5. grocery_stocker
    Replies:
    9
    Views:
    823
    grocery_stocker
    May 24, 2008
Loading...

Share This Page