alias and subclasses

T

Tim Clark

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
 
D

Daniel Berger

Tim said:
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
 
G

Gregory Brown

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top