class String
def self.inherited(subclass)
puts “#{self} was inherited by #{subclass}”
end
end
class MyString < String; end
输出:
String was inherited by MyString
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
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有