Hi David,
In mail "[ruby-talk:6264] aliasing class methods"
David Alan Black <dblack / candle.superlink.net> wrote:
> Hello --
>
> Is there more compact way to alias class methods than this
> (i.e., without the necessity of "class << self ... end")?
This is a little "illegal" way, but usable:
class T
def T.a
puts 'a called'
end
instance_eval "alias b a"
end
T.a #-> a called
T.b #-> a called
And we can make this code more compact by the following way:
class Module
def alias_class_method( new, old )
instance_eval "alias #{new.id2name} #{old.id2name}"
# or "instance_eval { alias_method new, old }"
end
end
class T
alias_class_method :c, :a
end
T.c #-> a called
> In a related vein: what functionality does alias_method add to alias?
> I've been playing with both, and in cases where either works, both
> seem to work (even if with slightly different syntax).
In a word, "alias" is required to aliasing global variables.
"alias_method" is required to using in method.
Minero Aoki