[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how to aliase a module method?

Xavier Noria

3/8/2007 10:34:00 AM

I am trying to aliase a module method to some local alias (to ease
some template). I've tried a few variations of

alias escape_latex MyUtils.escape_latex

without luck so far. I could mixin the module or write a wrapper, but
I'd like to know how to accomplish that with alias, or that it is not
possible if that's the case.

-- fxn




6 Answers

dseiler

3/8/2007 11:07:00 PM

0

On Mar 8, 5:34 am, Xavier Noria <f...@hashref.com> wrote:
> I am trying to aliase a module method to some local alias (to ease
> some template). I've tried a few variations of
>
> alias escape_latex MyUtils.escape_latex
>
> without luck so far. I could mixin the module or write a wrapper, but
> I'd like to know how to accomplish that with alias, or that it is not
> possible if that's the case.

It's possible, sort of. Try

module TheModule
instance_eval "alias escape_latex MyUtils.escape_latex"
end

I use a similar technique (originally suggested by Minero Aoki,
according to my notes) to replace Thread.critical and
Thread.critical=. I've only tested it in 1.8.2 but I don't think
anything's broken it since then.

> -- fxn


Ara.T.Howard

3/8/2007 11:16:00 PM

0

Jano Svitok

3/8/2007 11:18:00 PM

0

On 3/8/07, Xavier Noria <fxn@hashref.com> wrote:
> I am trying to aliase a module method to some local alias (to ease
> some template). I've tried a few variations of
>
> alias escape_latex MyUtils.escape_latex
>
> without luck so far. I could mixin the module or write a wrapper, but
> I'd like to know how to accomplish that with alias, or that it is not
> possible if that's the case.

first of all, you need to use symbols for methods (actually, you just
refer to the names of the methods):

alias :escape_latex :MyUtils.escape_latex

Can you see the problem: how you'd write the symbol for
MyUtils.escape_latex? This is not the way.
Now, this is excerpt from Ryan Davis' Ruby QuickRef
(http://www.zenspider.com/Languages/Ruby/Qui...):

Aliasing

alias :new :old
alias_method :new, :old

Creates a new reference to whatever old referred to. old can be any
existing method, operator, global. It may not be a local, instance,
constant, or class variable.

The simple solution would be in this case (provided you don't mind
including the other methods of the module):

include MyUtils

PS: I've tried

module TheModule
instance_eval "alias escape_latex MyUtils.escape_latex"
end

and I got:

(eval):1: parse error, unexpected '.', expecting $
alias escape_latex MyUtils.escape_latex
^(pointing at the dot)

I don't think it'd work, but I may be wrong, and will be glad if somebody
will show the working code for this.

Xavier Noria

3/9/2007 2:04:00 AM

0

On Mar 9, 2007, at 12:17 AM, Jan Svitok wrote:

> first of all, you need to use symbols for methods (actually, you just
> refer to the names of the methods):

No, that's not true.

-- fxn




Xavier Noria

3/9/2007 2:33:00 AM

0

On Mar 9, 2007, at 12:10 AM, dseiler@etceteraedutainment.com wrote:

> On Mar 8, 5:34 am, Xavier Noria <f...@hashref.com> wrote:
>> I am trying to aliase a module method to some local alias (to ease
>> some template). I've tried a few variations of
>>
>> alias escape_latex MyUtils.escape_latex
>>
>> without luck so far. I could mixin the module or write a wrapper, but
>> I'd like to know how to accomplish that with alias, or that it is not
>> possible if that's the case.
>
> It's possible, sort of. Try
>
> module TheModule
> instance_eval "alias escape_latex MyUtils.escape_latex"
> end
>
> I use a similar technique (originally suggested by Minero Aoki,
> according to my notes) to replace Thread.critical and
> Thread.critical=. I've only tested it in 1.8.2 but I don't think
> anything's broken it since then.

I don't understand that technique (it is not working here). In fact
what I don't understand is alias.

In irb we can do

alias foo sprintf

Why can't we say

alias foo Math.sqrt

? Having a quick glance at eval.c I think the problem is that alias
is syntax (that's why it does not need a comma between its
"arguments") and I guess here's the key:

case NODE_ALIAS:
if (NIL_P(ruby_class)) {
rb_raise(rb_eTypeError, "no class to make alias");
}
rb_alias(ruby_class, rb_to_id(rb_eval(self, node->u1.node)),
rb_to_id(rb_eval(self, node->u2.node)));

-- fxn


Rick DeNatale

3/9/2007 4:20:00 PM

0

On 3/8/07, Xavier Noria <fxn@hashref.com> wrote:
> On Mar 9, 2007, at 12:10 AM, dseiler@etceteraedutainment.com wrote:
>
> > On Mar 8, 5:34 am, Xavier Noria <f...@hashref.com> wrote:
> >> I am trying to aliase a module method to some local alias (to ease
> >> some template). I've tried a few variations of
> >>
> >> alias escape_latex MyUtils.escape_latex
> >>
> >> without luck so far. I could mixin the module or write a wrapper, but
> >> I'd like to know how to accomplish that with alias, or that it is not
> >> possible if that's the case.
> >
> > It's possible, sort of. Try
> >
> > module TheModule
> > instance_eval "alias escape_latex MyUtils.escape_latex"
> > end
> >
> > I use a similar technique (originally suggested by Minero Aoki,
> > according to my notes) to replace Thread.critical and
> > Thread.critical=. I've only tested it in 1.8.2 but I don't think
> > anything's broken it since then.
>
> I don't understand that technique (it is not working here). In fact
> what I don't understand is alias.
>
> In irb we can do
>
> alias foo sprintf
>
> Why can't we say
>
> alias foo Math.sqrt

Because alias requires that both arguments be names (or symbols), Note
that it's not a method but is directly performed by the ruby
interpreter.

irb(main):001:0> alias foo Math.sqrt
SyntaxError: compile error
(irb):1: parse error, unexpected '.', expecting $
alias foo Math.sqrt
^
from (irb):1

You're not even getting to eval.c, it's failing to parse.

Module#alias_method requires that it's aruments be symbols
cooresponding to the name.

The other problem is that you are trying to make a name in a different
scope than the name you are aliasing. As far as I can see this isn't
possible.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...