# Alias and subclasses

**URL:** <https://www.ruby-forum.com/t/alias-and-subclasses/107670>\
**Category:** Ruby\
**Created:** [June 25, 2007, 4:36am UTC](https://www.ruby-forum.com/t/alias-and-subclasses/107670 "2007-06-25T04:36:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [June 25, 2007, 4:36am UTC](https://www.ruby-forum.com/t/alias-and-subclasses/107670/1 "2007-06-25T04:36:30Z")

</div>

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 C.

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [June 25, 2007, 5:09am UTC](https://www.ruby-forum.com/t/alias-and-subclasses/107670/2 "2007-06-25T05:09:33Z")

</div>

Tim C. 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 [Monkey patching and aliases - Testing 1,2,3... — LiveJournal](http://djberg96.livejournal.com/97738.html) if  
anyone is interested.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [June 25, 2007, 5:14pm UTC](https://www.ruby-forum.com/t/alias-and-subclasses/107670/3 "2007-06-25T17:14:43Z")

</div>

On 6/24/07, Tim C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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,

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [June 25, 2007, 6:02pm UTC](https://www.ruby-forum.com/t/alias-and-subclasses/107670/4 "2007-06-25T18:02:59Z")

</div>

On 6/25/07, Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
