-
Book Overview & Buying
-
Table Of Contents
Polished Ruby Programming - Second Edition
By :
One of the great aspects of Ruby is how easy Ruby makes it to implement DSLs. After programmer friendliness, probably the main reason you see so many DSLs in Ruby is the simplicity of implementation. There are a few different DSL types you learned about in the previous sections, and you'll learn how to implement each in this section.
The first type is the most basic type, where the DSL method accepts a block that is yielded an object, and you call methods on the yielded object. For example, here's how you could implement the RSpec configuration example:
def RSpec.configure
yield RSpec::Core::Configuration.new
end
In this case, the configuration is global and always affects the RSpec constant, so the RSpec::Core::Configuration instance does not need a reference to the receiver (RSpec), which is why no arguments are passed to the new method.
For the Foo.process_bars example given previously, assuming the ProcessBarCommand uses the add_bar method and the...
Change the font size
Change margin width
Change background colour