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

你可能感兴趣的:(Ruby,Access)