Created
July 1, 2012 07:31
-
-
Save guange2015/3027340 to your computer and use it in GitHub Desktop.
扁平化作用域
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CleanRoom | |
| def run(setups, &block) | |
| setups.each do |setup| | |
| instance_eval &setup | |
| end | |
| instance_eval &block | |
| end | |
| end | |
| def init | |
| events ||= {} | |
| setups ||= [] | |
| 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 do |name, event| | |
| CleanRoom.new.run(setups) do | |
| block.call name, event | |
| end | |
| end | |
| end | |
| end | |
| init | |
| setup do | |
| puts "Setting up sky" | |
| @sky_height = 260 | |
| end | |
| setup do | |
| puts "Setting up mountaints" | |
| @mountains_height = 200 | |
| end | |
| event 'the sky is falling' do | |
| @sky_height < 300 | |
| end | |
| event "it's getting closer" do | |
| @sky_height < @mountains_height | |
| end | |
| each_event do |name, event| | |
| puts name if self.instance_eval(&event) | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment