O'Reilly logo

Metaprogramming Ruby 2 by Paolo Perrotta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Quiz: A Better DSL

Where you’re unexpectedly left alone to develop a new version of the RedFlag DSL.

Your boss wants you to add a setup instruction to the RedFlag DSL, as shown in the following code.

blocks/redflag_3/events.rb
 
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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required