Can Tap be used to write more flexible code?

I've been looking at some examples on how tap can be used, and I'm not sure how it can be used to write flexible code

For example

http://blog.moertel.com/posts/2007-02-07-ruby-1-9-gets-handy-new-method-object-tap.html

http://moonbase.rydia.net/mental/blog/programming/eavesdropping-on-expressions

Given that RM scripting is mostly about chain aliasing, and tap is meant to help with chain calling, it seems like something can be done here.

So say I had a method that adds two numbers together

def my_sum(a, a + bendAnd then I wanted to add some logic, I could say
Code:
alias :th_my_sum :my_sumdef my_sum(a,   res = th_my_sum(a,   p res  resend
Or I could say
Code:
alias :th_my_sum :my_sumdef my_sum(a,   th_my_sum(a, .tap {|res| p res }end
But this is a trivial example and isn't something that I would be putting in released scripts.Are there any use cases where tap would provide MORE flexibility over simply aliasing?
Yes, your example doesn't gain a lot from the use of tap; But in more complex cases, code can benefit from greater fluency through method chaining.

For example, you can turn this:

def do_work(workload, arg) log(workload) reduced = workload.reduce(&:*) string = reduced.to_s log(string) result = yield string return resultend def log(*args) ...endinto this:

def do_work(workload, arg) yield workload .tap(&method🙂log)) # log the workload .reduce(&:*) # >> 24 .call(&:to_s) # >> "24" .tap(&method🙂log)) # log the stringendIt certainly doesn't make your code more flexible, but it can do wonders for readability.  Just a nice peace of syntactic sugar for most people.
So the tap method doesn't actually provide more power, like being able to literally hijack a block and inject all sorts of extra stuff and forcing it to pass that extra stuff down.

Because I see a lot of code like this

some_arr.each do |elmt| do_something_to_elmtendAnd I'd like to be able to hijack that block without actually changing the existing method.Initially I thought maybe tap could do that but then after reading the examples, it's mostly just syntax.
Essentially, you can think of #tap as shorthand for calling the given block and returning self -- that's really it. It doesn't offer any more power than that. That doesn't mean that it isn't useful, though -- it certainly can be, especially for chaining methods to a desired object when the original method doesn't return the receiver (as you noted).


But no, it's not black magic.
Rails has a variant of it called ``returning``. Most of the time I use it to drop into a chain and find out what's in there.

You do realize though that each returns the collection right? It's an Enumerable method, meaning it can be chained with others of the same kind perpetually (except for ones like reduce of course.)

Now if you're wanting to inject things into a block, you might check out something I had made a while ago: https://github.com/baweaver/pipeable

Essentially all it is is this:

class Object  def pipe(&block)    yield self  endend
...and you can do this:

1.pipe { |x| "#{x} is great!" }# => '1 is great!'
Now if you're looking at how to warp around blocks and do some nifty injection logic, here are a few concepts to look into:

  • Partial Application
  • Currying
  • Functional Composition
  • Combinators
  • Monads (Very advanced though)
One of the best books for a simple introduction to this is: http://www.amazon.com/Functional-JavaScript-Introducing-Programming-Underscore-js/dp/1449360726

As far as a far more advanced one that takes you through combinators and will trip your mind on what Ruby can do: https://leanpub.com/combinators

Original thread: https://forums.rpgmakerweb.com/threads/can-tap-be-used-to-write-more-flexible-code.34881/