# Alias for a class?

**URL:** <https://www.ruby-forum.com/t/alias-for-a-class/70312>\
**Category:** Ruby\
**Created:** [September 28, 2006, 4:32am UTC](https://www.ruby-forum.com/t/alias-for-a-class/70312 "2006-09-28T04:32:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hopscotch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@hopscotch](https://www.ruby-forum.com/u/hopscotch)\
**Post date:** [September 28, 2006, 4:32am UTC](https://www.ruby-forum.com/t/alias-for-a-class/70312/1 "2006-09-28T04:32:41Z")

</div>

Hi all,

Last night I was playing with some third-party lib and found myself  
having to type StupidlyLongLibName::LongModuleName::LongClassName  
repeatedly and I wanted a mechanism where I could rename these classes  
for my own use to cut down on the finger typing.

I thought I could just do:

class Shortname \< OldLongClassName; end

Unfortunately this didn’t work (yes there’s no error thrown, but the  
later code expected OldLongClassName and wouldn’t work with Shortname

So my request is:  
a: is it possible to alias a class (I know you can do it with methods)?  
b: if not can it be added (perhaps with aka)?

Thanks  
Kev

---

<div class="post-metadata">

**Author:** ![hopscotch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@hopscotch](https://www.ruby-forum.com/u/hopscotch)\
**Post date:** [September 28, 2006, 4:36am UTC](https://www.ruby-forum.com/t/alias-for-a-class/70312/2 "2006-09-28T04:36:43Z")

</div>

Kevin J. wrote:

> a: is it possible to alias a class (I know you can do it with methods)?  
> SomeClass = SomeOtherClass

---

<div class="post-metadata">

**Author:** ![hopscotch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@hopscotch](https://www.ruby-forum.com/u/hopscotch)\
**Post date:** [September 28, 2006, 8:55am UTC](https://www.ruby-forum.com/t/alias-for-a-class/70312/3 "2006-09-28T08:55:14Z")

</div>

Kevin J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So my request is:  
> a: is it possible to alias a class (I know you can do it with  
> methods)? b: if not can it be added (perhaps with aka)?

You can store that class in any variable or constant you like. So all  
of  
these work:

$cl = ::StupidlyLongLibName::LongModuleName::LongClassName  
@cl = ::StupidlyLongLibName::LongModuleName::LongClassName  
@@cl = ::StupidlyLongLibName::LongModuleName::LongClassName  
cl = ::StupidlyLongLibName::LongModuleName::LongClassName  
Cl = ::StupidlyLongLibName::LongModuleName::LongClassName

Note: depending on where in the code you are some of these do not really  
make sense.

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![hopscotch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@hopscotch](https://www.ruby-forum.com/u/hopscotch)\
**Post date:** [September 28, 2006, 10:57am UTC](https://www.ruby-forum.com/t/alias-for-a-class/70312/4 "2006-09-28T10:57:23Z")

</div>

On 9/28/06, Kevin J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Unfortunately this didn’t work (yes there’s no error thrown, but the  
> later code expected OldLongClassName and wouldn’t work with Shortname
> 
> So my request is:  
> a: is it possible to alias a class (I know you can do it with methods)?  
> b: if not can it be added (perhaps with aka)?

You can do this as well:

include StupidlyLongLibName::LongModuleName

however it all stuff from the said module.

---

<div class="post-metadata">

**Author:** ![hopscotch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@hopscotch](https://www.ruby-forum.com/u/hopscotch)\
**Post date:** [September 28, 2006, 9:01am UTC](https://www.ruby-forum.com/t/alias-for-a-class/70312/5 "2006-09-28T09:01:15Z")

</div>

> Note: depending on where in the code you are some of these do not really  
> make sense.

That’s an amazingly intuitive comment - wish I’d made that /me ==  
jealous…

Thanks for the rest of the insight, but this particular thing rings  
true for just about any/all code

Kev

---

<div class="post-metadata">

**Author:** ![hopscotch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@hopscotch](https://www.ruby-forum.com/u/hopscotch)\
**Post date:** [September 28, 2006, 4:48pm UTC](https://www.ruby-forum.com/t/alias-for-a-class/70312/6 "2006-09-28T16:48:16Z")

</div>

removed\_email\_address@domain.invalid wrote:

> my favourite is
> 
> d, f, fu = Dir, File, FileUtils
> 
> since those three go together in verbose code so often.

w00t! Forget all those silly “constants”!

```
$d, $f, $fu = Dir, File, FileUtils

```

T.

---

<div class="post-metadata">

**Author:** ![hopscotch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@hopscotch](https://www.ruby-forum.com/u/hopscotch)\
**Post date:** [September 28, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/alias-for-a-class/70312/7 "2006-09-28T16:26:05Z")

</div>

On Thu, 28 Sep 2006, Robert K. wrote:

> @@cl = ::StupidlyLongLibName::LongModuleName::LongClassName  
> cl = ::StupidlyLongLibName::LongModuleName::LongClassName  
> Cl = ::StupidlyLongLibName::LongModuleName::LongClassName
> 
> Note: depending on where in the code you are some of these do not really make  
> sense.

my favourite is

d, f, fu = Dir, File, FileUtils

since those three go together in verbose code so often.

-a

---

<div class="post-metadata">

**Author:** ![hopscotch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@hopscotch](https://www.ruby-forum.com/u/hopscotch)\
**Post date:** [September 28, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/alias-for-a-class/70312/8 "2006-09-28T18:42:55Z")

</div>

On 9/28/06, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> w00t! Forget all those silly “constants”!
> 
> ```
> $d, $f, $fu = Dir, File, FileUtils
> 
> ```

Much too readable

\*x = Dir, File, FileUtils

who needs names!!!

Robert

T.
