classPostattr_reader:foo#=> return @foo#一定要在定義好的 method 後面還 call 得到alias_method:bar,:foo#=> the same method 別名/原名definitialize(foo=[])@foo=fooendend
可改成 alias
123
alias_method:bar,:foo#上下相等aliasbarfoo
alias_attribute
1234567891011121314
classContent<ActiveRecord::Base# has a title attributeendclassEmail<Contentalias_attribute:subject,:titleende=Email.find(1)e.title# => "Superstars"e.subject# => "Superstars"e.subject?# => truee.subject="Megastars"e.title# => "Megastars"
classMethodLoggerdeflog_method(klass,method_name)klass.class_evaldoalias_method"#{method_name}_original",method_namedefine_methodmethod_namedo|*args,&block|puts"#{Time.now}: Called #{method_name}"send"#{method_name}_original",*args,&blockendendendendclassPostdefsay_hiputs"Hi"endendlogger=MethodLogger.newlogger.log_method(Post,:say_hi)Post.new.say_hi#=> 2016-04-05 12:14:01 +0800: Called say_hi#=> Hi