[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Alias for a class?

Kev Jackson

9/28/2006 2:32:00 AM

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

6 Answers

Devin Mullins

9/28/2006 2:36:00 AM

0

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

Robert Klemme

9/28/2006 6:53:00 AM

0

Kevin Jackson <foamdino@gmail.com> 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

Kev Jackson

9/28/2006 6:59:00 AM

0

> 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

Jano Svitok

9/28/2006 8:56:00 AM

0

On 9/28/06, Kevin Jackson <foamdino@gmail.com> wrote:
> 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)?

You can do this as well:

include StupidlyLongLibName::LongModuleName

however it all stuff from the said module.

Ara.T.Howard

9/28/2006 2:25:00 PM

0

Trans

9/28/2006 2:46:00 PM

0


ara.t.howard@noaa.gov 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.