This code is extracted/adapted from Mat Brown's Sunspot gem. One of Sunspot's nicest features is an expressive DSL for defining search indexes and performing queries. It works by instance_eval
-ing a block you pass into it in the context of its own search builder object. In this code, the pig thing1
statement is roughly equivalent to zoo = Zoo.new; zoo.pig(thing1)
.
Sunspot's DSL has to resort to trickery: the instance_eval_with_context
method uses eval
to get the block to give up the object it considers to be self
, then sets up an elaborate system of delegates and method_missing
calls so any methods not handled by the DSL are forwarded to the surrounding object. But as a result, this syntax is minimal and beautiful, and it works the way you expect whether or not you prefer blocks to yield an object.
Without this trick the block would be restricted to either the original, "calling" context (as a closure) or the DSL's "receiving" context (using instance_eval
), but not both.