Ruby’s Object#tap IS AWESOME

Oops….

s/IS AWESOME/considered harmful/

Much better.

This is exactly why Ruby gets to be too much for big projects that require long-term maintenance. This insane need to reduce lines of code. It’s beyond an obsession, it’s become a primal urge that Ruby devs can’t fight. Somewhere along the lines, the Ruby goal of “elegance” turned into “magic”.

What is Object#tap? It’s a way to type less code. Just like 90% of the extraneous bullshit in Ruby.

You tap an object to get a block with said object passed in, and where the object is returned from the block.

# Turn this terribly ugly example code...
def thing(data, input)
  data[:foo] = "bar"
  data.process(input)
  return data
end

# Into this beautiful one-liner
def beautiful_thing(data, input)
  return data.tap {|d| d[:foo] = "bar"}.tap{|d| d.process(input)}
end

I dunno, man. To me that just seems like a lovely way to make your code look like shit… all in the name of saving a few lines of more clear, explicit code.

2 Replies to “Ruby’s Object#tap IS AWESOME”

  1. I feel that way with many of the newer additions to ruby anyway, not only tap.

    Good thing is that I can ignore them.

    Frozen strings by default on the other hand, now THAT is really something that annoys me to no ends. To force me to rewrite all my old code, to permanently switch away from something I used for +10 years… nah. I can already see this being the dealbreaker for me. Unfortunately, there aren’t enough languages like ruby out there …

    1. Ignoring is easy when it’s your own code – and I still somewhat enjoy Ruby for small personal projects. But when working with a team where “do everything the latest cool way in as few lines as possible” is the mantra, it’s actually sometimes a necessary evil to learn the stuff I’d rather just pretend never happened.

      Haven’t been bitten by frozen strings, but then I tend to avoid Ruby whenever possible these days :( I find Python obnoxious in many ways, but I do prefer the general community belief that magic should be kept at a minimum, and code should be readable above all other concerns.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.