InfoQ

News

Tapping method chains with Ruby 1.9

Posted by Werner Schuster on Feb 04, 2008 04:00 PM

Community
Ruby
Topics
Debugging,
Programming
Tags
Functional Programming
The idea for the tap method has been around for some time - but it has now been added to the standard Ruby library in Ruby 1.9. MenTaLguY, who blogged about the idea behind tap shows the simple code:
class Object
 def tap
 yield self
  self
 end
end

In Ruby 1.9, the tap method is defined in Object, making it available for every object in Ruby by default. The method takes a Block as argument, which it calls with self as argument - then the object is returned.

The indirection through the tap method seems like a complicated way of doing something with an object. The real benefit of this becomes clear when the object of interest is passed from one method to another without ever being assigned to a variable. This is common whenever methods are chained, particularly if the chain is long.
An example: without tap, a temporary variable is needed:
xs = blah.sort.grep( /foo/ )
p xs
# do whatever we had been doing with the original expression
xs.map { |x| x.blah }

With tap:
blah.sort.grep( /foo/ ).tap { |xs| p xs }.map { |x| x.blah } 

This shows where tap is useful: without it, it's necessary to assign the object of interest to a local variable to use it - with tap it's possible to insert the Block that inspects the object right where the handover between the chained methods happens. This gets particularly useful with APIs that expose so called Fluent Interfaces - i.e. APIs that encourage method chaining. Here a Java example from Martin Fowler's website:
customer.newOrder()
 .with(6, "TAL")
 .with(5, "HPK").skippable()
 .with(3, "LGV")
 .priorityRush();

In case this code has a bug, tap allows to look at the object at an arbitrary stage (i.e. between every call) by simply inserting a tap block. This is also useful with debugging tools, which often don't support looking at anonymous return values of methods.

It's important to mention that tap is normally about causing some kind of side effect without changing the object (the Block's return value is ignored). However, it is of course possible to modify the object as long as it's mutable.

Users of Rails' ActiveSupport are already familiar with a similar method in the form of the returning method .

Of course, the tap method is not restricted to Ruby 1.9 - Ruby's Open Classes allow to do this on non-1.9 Ruby versions too.

No comments

Reply

Exclusive Content

Rationalizing the Presentation Tier

Thin client paradigm characterized by web applications is a kludge that needs to be repudiated. Old compromises are no longer needed and it's time to move the presentation tier to where it belongs.

Agile Project Management: Lessons Learned at Google

In this presentation filmed during QCon 2007, Jeff Sutherland, the creator of Scrum, talks about his visit at Google to do an analysis of Google's first implementation of Scrum.

AtomServer – The Power of Publishing for Data Distribution

In this article, Bryon Jacob and Chris Berry introduce AtomServer, their implementation of a full-fledged Atom Store based on Apache Abdera, which is now available as open source.

An Introduction to Virtualization

It is easy to think that virtualization applies only to servers. In reality the recent resurgence of the concept is also being applied to networking, storage, and application infrastructure.

REST Anti-Patterns

In this article, Stefan Tilkov explains some of the most common anti-patterns found in applications that claim to follow a "RESTful" design and suggests ways to avoid them.

Choosing between Routing and Orchestration in an ESB

In this article, Adrien Louis and Marc Dutoo discuss the differences and relative merits of using orchestration vs. routing in a typical ESB setup, and discuss various implementation options.

Enterprise Batch Processing with Spring

Wayne Lund discusses batch processing, Spring Batch objectives and features, scenarios for usage, Spring Batch architecture, scaling, example code, failures and retrying, and the future roadmap.

User Story Estimation Techniques

Developer Jay Fields draws on his experiences as a ThoughtWorks consultant to describe effective user story estimation techniques.