Forum: Ruby alias and subclasses

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6258cc6925be1794f3f5d5b5e75f5694?d=identicon&s=25 Tim Clark (timosky)
on 2007-06-25 04:36
(Received via mailing list)
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
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2007-06-25 05:09
(Received via mailing list)
Tim Clark wrote:
>     def unreserve
>
> 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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2007-06-25 17:14
(Received via mailing list)
On 6/24/07, Tim Clark <tim@bass.net.au> wrote:
> class Ticket
> end
> following?
> class Ticket
>         def unreserve
>                 ...
>         end
>
>         def unhold
>                 unreserve
>         end
> end


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? :) )

Cheers,
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-06-25 18:02
(Received via mailing list)
On 6/25/07, Logan Capaldo <logancapaldo@gmail.com> 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 :old_something, :something

def something
  #do some hackery
  old_something
end
This topic is locked and can not be replied to.