實(shí)例講解Ruby中的鉤子方法及對(duì)方法調(diào)用添加鉤子
鉤子方法有些類似事件驅(qū)動(dòng)裝置,可以在特定的事件發(fā)生后執(zhí)行特定的回調(diào)函數(shù),這個(gè)回調(diào)函數(shù)就是鉤子方法(更形象的描述: 鉤子方法可以像鉤子一樣,勾住一個(gè)特定的事件。),在Rails中before\after函數(shù)就是最常見的鉤子方法。
Class#inherited方法也是這樣一個(gè)鉤子方法,當(dāng)一個(gè)類被繼承時(shí),Ruby會(huì)調(diào)用該方法。默認(rèn)情況下,Class#inherited什么都不做,但是通過(guò)繼承,我們可以攔截該事件,對(duì)感興趣的繼承事件作出回應(yīng)。
class String def self.inherited(subclass) puts “#{self} was inherited by #{subclass}” end end class MyString < String; end輸出:
String was inherited by MyString
通過(guò)使用鉤子方法,可以讓我們?cè)赗uby的類或模塊的生命周期中進(jìn)行干預(yù),可以極大的提高編程的靈活性。
對(duì)方法調(diào)用添加鉤子的實(shí)例
ruby有很多有用的鉤子,如included,inhered,以及method_missing。對(duì)方法調(diào)用添加鉤子可以用alias環(huán)繞別名實(shí)現(xiàn),但終歸有些麻煩,alias_method_chain需要定義with_feature方法也較麻煩,因此實(shí)現(xiàn)了下列module,include后調(diào)用method_callback :before_method,:after_method即可為before_method添加after_method鉤子
module AfterCall def self.included(base) base.extend(ClassMethods) end module ClassMethods def after_call when_call,then_call,*args_then,&block_then alias_method "old_#{when_call}",when_call define_method when_call do |*args_when,&block_when| send "old_#{when_call}",*args_when,&block_when send then_call,*args_then,&block_then end end end end class Student include AfterCall def enter_class sb puts "enter class #{sb}" yield('before') if block_given? end private def after_enter_class pop puts "after enter class #{pop}" yield('after') if block_given? end protected def third_after puts "from third enter" end after_call :after_enter_class ,:third_after after_call :enter_class ,:after_enter_class,"doubi", &lambda {|x|puts "from lambda #{x}"} end Student.new.enter_class "1" do |x| puts "from lambda #{x}" end
運(yùn)行結(jié)果如下:
#enter class 1 #from lambda before #after enter class doubi #from lambda after #from third enter
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。