当前位置: 代码网 > it编程>前端脚本>Ruby > 实例讲解Ruby中的钩子方法及对方法调用添加钩子

实例讲解Ruby中的钩子方法及对方法调用添加钩子

2024年05月18日 Ruby 我要评论
钩子方法有些类似事件驱动装置,可以在特定的事件发生后执行特定的回调函数,这个回调函数就是钩子方法(更形象的描述: 钩子方法可以像钩子一样,勾住一个特定的事件。),在rails中beforeafter

钩子方法有些类似事件驱动装置,可以在特定的事件发生后执行特定的回调函数,这个回调函数就是钩子方法(更形象的描述: 钩子方法可以像钩子一样,勾住一个特定的事件。),在rails中before\after函数就是最常见的钩子方法。

class#inherited方法也是这样一个钩子方法,当一个类被继承时,ruby会调用该方法。默认情况下,class#inherited什么都不做,但是通过继承,我们可以拦截该事件,对感兴趣的继承事件作出回应。

class string
  def self.inherited(subclass)
    puts “#{self} was inherited by #{subclass}”
  end
end
class mystring < string; end
输出:
string was inherited by mystring

通过使用钩子方法,可以让我们在ruby的类或模块的生命周期中进行干预,可以极大的提高编程的灵活性。

对方法调用添加钩子的实例
ruby有很多有用的钩子,如included,inhered,以及method_missing。对方法调用添加钩子可以用alias环绕别名实现,但终归有些麻烦,alias_method_chain需要定义with_feature方法也较麻烦,因此实现了下列module,include后调用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

运行结果如下:

#enter class 1
#from lambda before
#after enter class doubi
#from lambda after
#from third enter

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com