下面是代码,
event "the sky is falling" do
@sky_height < 300
end
event "it's getting closer" do
@sky_height < @mountains_height
end
setup do
puts "Setting up sky"
@sky_height = 100
end
setup do
puts "Settig up mountains"
@mountains_height = 200
end
def event(name, &block)
@events[name] = block
end
def setup(&block)
@setups << block
end
Dir.glob('file1.rb').each do |file|
@setups = []
@events = {}
load file
@events.each_pair do |name, event|
env = Object.new
@setups.each do |setup|
env.instance_eval &setup
end
puts "ALERT: #{name}" if env.instance_eval &event
end
end
Setting up sky Settig up mountains ALERT: the sky is falling Setting up sky Settig up mountains ALERT: it's getting closer
问题来了,在 main.rb 中,先调用的 load file, 应该先去执行 file1.rb 里面的方法,然后再执行下去,有点看不懂,求指点...