alias迷思

对alias还不太熟悉,看到下面的代码有点晕了..

class Adaptee
def talk; puts 'Adaptee';end
end
class Adapter < Adaptee
alias talkee talk
def talk
puts 'before Adaptee'
talkee
puts 'after Adaptee'
end
end
Adapter.new.talk



迷惑1:alias talkee talk是把Adapter#talk还是Adaptee#talk拷贝一份并起个新名字talkee呢?
迷惑2:alias talkee talk的位置,放在下面会如何呢?
迷惑3:怎么感觉这个程序会无限递归呢(实际没有..)

都说*nix下有事找男人(man),学ruby有事没事日(ri)一下...

---------------------------------------------------- Module#alias_method
alias_method(new_name, old_name) => self
------------------------------------------------------------------------
Makes _new_name_ a new copy of the method _old_name_. This can be
used to retain access to methods that are overridden.

module Mod
alias_method :orig_exit, :exit
def exit(code=0)
puts "Exiting with code #{code}"
orig_exit(code)
end
end
include Mod
exit(99)

_produces:_

Exiting with code 99


这个例子更让人迷惑..本来有继承关系的时候就迷糊了,现在这个例子又多出来一个exit(Kernel下定义的方法)

于是只好自己简化一些:


module TestAlias
alias _say say
def say
puts "Hello"
end
end
include TestAlias
_say
#undefined method `say' for module `TestAlias' (NameError)


module TestAlias
def say
puts "Hello"
end
alias _say say
end
include TestAlias
_say
#hello

结论1:alias和位置有关要alias的方法必须之前有定义..
继续折腾:

def say
puts "From Top Level"
end
module TestAlias
alias _say say
def say
puts "Hello"
end
end
include TestAlias
_say
#From Top Level

结论:alias 会沿着继承链(父类,Mix-in,Kernel,Object,Top Level等)查找方法直到找不到方法时抛出No Method Error异常。(上面只举了一个Top Level的例子,其他情况类似)
最后自己hack了一下alias_method:


class Module
alias _alias_method alias_method
def alias_method alias_m, m
define_method m do
self.__send__ m
end
_alias_method alias_m, m
end
end

class TT
#让alias_method放在前面也可以
alias_method :_say, :say
def say
puts "say"
end
end
TT.new._say
#say

PS:感觉最开始的代码用super比较不错..

class Adaptee
def talk; puts 'Adaptee';end
end
class Adapter < Adaptee
#alias talkee talk
def talk
puts 'before Adaptee'
super
puts 'after Adaptee'
end
end
Adapter.new.talk
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值