Came across a need for this in a somewhat silly golfing exercise, but
I was wondering if there was any real use for it, and if it had a name
in some language somewhere. It essentially lets a block of code be
inserted into a method chain (a la Object#tap) but returns the return
value of the block rather than the passed in argument.
for example (using “apply” for want of a better name)
array.inject({}).map {|h, e| h[e] = f(e); h}
would become
array.map {|e| e, f(e)}.flatten.apply {|x| Hash[*x]}
Having thought about it I still like ‘apply’. It doesnt have the
connotation that the object itself is being changed (unlike, e.g.
‘morph’ from that discussion), and it’s fairly close to the lisp
meaning of (apply f x) => (f x)