Quiz: A Better DSL
Where you’re left alone to develop a new version of the RedFlag DSL.
Your boss wants you to add a setup instruction to the RedFlag DSL:
| setup do |
| puts "Setting up sky" |
| @sky_height = 100 |
| end |
| |
| setup do |
| puts "Setting up mountains" |
| @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 |
| |
| event "whoops... too late" do |
| @sky_height < 0 |
| end |
In this new version of the DSL, you’re free to mix events and setup blocks (setups for short). The DSL still checks events, and it also executes all the setups before each event. If you run redflag.rb on the previous ...
Get Metaprogramming Ruby 2 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.