Alias for a class?

K

Kevin Jackson

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
 
R

Robert Klemme

Kevin Jackson said:
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
 
K

Kevin Jackson

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
 
J

Jan Svitok

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

ara.t.howard

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.

my favourite is

d, f, fu = Dir, File, FileUtils

since those three go together in verbose code so often.

-a
 
T

Trans

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.
 

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

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top