Here’s a neat Ruby trick for fans of Symbol#to_proc.
This magical method, introduced by both Rails and Ruby 1.9, lets you do things like this:
1 2 | |
And that proc can, of course, take multiple parameters:
1 2 3 | |
However, if you want to “close” a value into the proc, you’re out of luck, so you have to define a block old-school-style:
1 2 | |
But check out the following little hack:
1 2 3 4 5 | |
Now look what you can do:
1 2 | |
What do you think? Handy? Disgusting? Leave a comment to let me know!
Update: apparently others have had similar ideas, but with different semantics. So perhaps this trick is a little too opaque…