不知道是不是作者学 js 太入神,完全是 js 搞法,为了隐藏一个全局变量,却搞出 N 多的全局方法出来了。
lambda {
setups = []
events = {}

Kernel.send :define_method, :event do |name, &block|
events[name] = block
end
Kernel.send :define_method, :setup do |&block|
setups << block
end
Kernel.send :define_method, :each_event do |&block|
events.each_pair do |name, event|
block.call name, event
end end
Kernel.send :define_method, :each_setup do |&block|
setups.each do |setup|
block.call setup
end end
}.call
Dir.glob('*events.rb').each do |file| load file
each_event do |name, event|
env = Object.new each_setup do |setup|
env.instance_eval &setup
end
puts "ALERT: #{name}" if env.instance_eval &event end
end
我稍微改了一下,看着顺眼多了,缺点就是调试定不到文件。
Class.new do
def event(name, &block)
@events ||= {}
@events[name] = block
end
def setup(&block)
@setups ||= []
@setups << block
end
def run
@events.each do |k,v|
o = Object.new
@setups.each do |setup|
o.instance_eval &setup
end
puts "ALERT: #{k}" if o.instance_eval &v
end
end
end.new.instance_eval {
instance_eval File.open("./test_event.rb").read
run
}