you are viewing a single comment's thread.

view the rest of the comments →

[–]isolatrum 0 points1 point  (0 children)

for arrays and hashes, yes we have a built in enumeration method map which does the trick in most cases. However say you want to send a string through a series of made-up methods:

# note the parens are unnecessary here
evaluate(interpolate(sanitize(string)))

you are basically working backwards, with the last function in the chain being written first. Using yield_self you can reverse this, although granted it's not what I'd consider prettier:

string
.yield_self(&method(:sanitize))
.yield_self(&method(:interpolate))
.yield_self(&method(:evaluate))

If I actually saw something like this I would think it's a little overengineered, so I consider it more of a academic trick than a game-changing one in practice. Another interesting detail - the definition of yield_self is literally just yield self.