Little Things: Introspecting Block Parameters
This week I stumbled across a rather esoteric feature of Ruby, but one that actually proved really, really useful with something I’m doing for a client of mine. The feature? Proc#parameters
. From the Ruby docs:
Since I don’t yet have permission from my client to share the actual code I wrote, I’ll demonstrate this esoteric little feature by monkeypatching Hash
and adding a using
method. The end result will let us access Hash keys that match the names of the parameters we pass to the block, like this:
(Admittedly, these examples are a bit contrived, but I’m really quite in love with this idea. Hopefully I get to share the real use case eventually!)
The implementation of #using
is delightfully simple:
That’s it! We just inspect the parameters of the block, map the parameter names to the corresponding values, and invoke the block with those values as arguments.
I love Ruby. Fifteen years, and going strong.