# Tap method : good or bad practice?

**URL:** <https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410>\
**Category:** Ruby\
**Created:** [April 21, 2013, 7:56am UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410 "2013-04-21T07:56:46Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 21, 2013, 7:56am UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/1 "2013-04-21T07:56:46Z")

</div>

Hi all !

I try to code a function that generates and returns options. Is it  
better to do this :

def options  
options = {}  
1.upto(31) { |i| options[i] = i }  
options  
end

Or this :

def options  
{}.tap { |options| 1.upto(31) { |i| options[i] = i } }  
end

What is the Ruby way of doing this ? Is it common to use this  
trap method ? Thank you very much for your help (I’m new to Ruby) !

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 21, 2013, 8:43am UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/2 "2013-04-21T08:43:53Z")

</div>

On Sun, Apr 21, 2013 at 12:56 AM, Sébastien Durand  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> What is the Ruby way of doing this ? Is it common to use this  
> trap method ? Thank you very much for your help (I’m new to Ruby) !

There is more than one way to do things.

def preset\_options(n)  
1.upto(n).reduce({}) {|o,i| o[i]=i; o}  
end

The above doesn’t need to be wrapped in a method, though if it is an  
oft-repeated motif, it should be.

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 21, 2013, 10:40am UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/3 "2013-04-21T10:40:42Z")

</div>

On Sun, Apr 21, 2013 at 8:43 AM, tamouse mailing lists \<  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:

> There is more than one way to do things.

That’s true for sure. Why write all the values into the Hash? Here’s  
my  
take:

options = Hash.new {|h,k| k}

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 21, 2013, 11:32am UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/4 "2013-04-21T11:32:31Z")

</div>

Robert K. wrote in post #1106422:

> On Sun, Apr 21, 2013 at 8:43 AM, tamouse mailing lists \<  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:
> 
> > There is more than one way to do things.
> 
> That’s true for sure. Why write all the values into the Hash? Here’s  
> my  
> take:
> 
> options = Hash.new {|h,k| k}
> 
> Kind regards
> 
> robert

Robert the Problem is the default proc, it is maybe not what he wants  
because it pretend from marshaling

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 21, 2013, 11:35am UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/5 "2013-04-21T11:35:01Z")

</div>

tap is an example of a K combinator. It might not be common practice,  
though some probably use it without knowing, but it is good practice.

Henry

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 21, 2013, 11:34am UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/6 "2013-04-21T11:34:26Z")

</div>

On Sun, 21 Apr 2013 07:56:50 +0200, Sébastien Durand  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def options  
> options = {}  
> 1.upto(31) { |i| options[i] = i }  
> options  
> end
> 
> def options  
> {}.tap { |options| 1.upto(31) { |i| options[i] = i } }  
> end

Well, which one looks easier to read and understand? For me it’s  
obviously the first one. Number of lines of code is not a metric of  
complexity.

On Sun, 21 Apr 2013 08:43:15 +0200, tamouse mailing lists  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def preset\_options(n)  
> 1.upto(n).reduce({}) {|o,i| o[i]=i; o}  
> end

Uhh, and please don’t do this, either. If you need multiple expressions  
inside reduce’s block, you’re doing it wrong.

If you really wanted to make it one expression, use a hash with default  
like Robert suggested, or something like `Hash[(1..31).map{|n| [n, n] } ]` - but frankly, your initial version looks the prettiest to me.

(On a completely unrelated note, because I felt like golfing: `Hash[(1..31).map &Array.method(:new).to_proc.curry(2).call(2)]`.)

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 21, 2013, 7:43pm UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/7 "2013-04-21T19:43:17Z")

</div>

To better express the above reduce case:

1.upto(n).each\_with\_object({}) { |i,mem| mem[i]=i }

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 21, 2013, 8:20pm UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/8 "2013-04-21T20:20:56Z")

</div>

Can on the same context anyone explain the difference between `tap` and  
`inject` ?

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 21, 2013, 11:18pm UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/9 "2013-04-21T23:18:15Z")

</div>

On Sun, Apr 21, 2013 at 11:32 AM, Hans M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > options = Hash.new {|h,k| k}
> 
> Robert the Problem is the default proc, it is maybe not what he wants  
> because it pretend from marshaling

Why is that a problem? Do you have knowledge about the situation of  
Sbastien which other writers in this thread do not have? If not, why  
are  
you claiming there is problem?

Cheers

robert

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 21, 2013, 4:42pm UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/10 "2013-04-21T16:42:49Z")

</div>

On Sun, Apr 21, 2013 at 4:32 AM, Bartosz Dziewoński  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Uhh, and please don’t do this, either. If you need multiple expressions  
> inside reduce’s block, you’re doing it wrong.

This is completely useless advice.

Why is it wrong?

The documentation has just such an example, as do many other places  
describing the method. Whence comes your stricture? I find nothing  
about it.

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 21, 2013, 11:52pm UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/11 "2013-04-21T23:52:44Z")

</div>

Regardless of the refactors people are suggesting, going to the original  
question about tap I know people who like it and people who dislike it.  
From my perspective I don’t think there’s consensus as to which form is  
preferred.

Most of the times I use tap, because it conveys the intention to the  
reader  
that you are going to return that value. It is kind of declarative for  
me.  
But in a method like yours where there’s three lines I would not  
normally  
use it, since I believe people load those 3 lines in one shot in their  
head  
and the form without tap is probably more readable.

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 22, 2013, 12:20am UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/12 "2013-04-22T00:20:46Z")

</div>

On Sun, Apr 21, 2013 at 12:56 AM, Sbastien D.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> What is the Ruby way of doing this ? Is it common to use this  
> trap method ? Thank you very much for your help (I’m new to Ruby) !

I wouldn’t do that, unless I had a need to inline it (e.g. writing a  
one-liner on the the command line), but I also wouldn’t care enough to  
change it if I saw it in my codebase. It is totally irrelevant next to  
concerns about object boundaries and mutability, so it seems to me that  
any  
disagreement is just bikeshedding.

On Sun, Apr 21, 2013 at 1:20 PM, Love U Ruby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Can on the same context anyone explain the difference between `tap` and  
> `inject` ?

Are the docs unclear?

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 22, 2013, 2:16pm UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/13 "2013-04-22T14:16:09Z")

</div>

On Apr 21, 2013 5:21 PM, “Josh C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Sun, Apr 21, 2013 at 12:56 AM, Sébastien Durand [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > What is the Ruby way of doing this ? Is it common to use this  
> > trap method ? Thank you very much for your help (I’m new to Ruby) !
> 
> I wouldn’t do that, unless I had a need to inline it (e.g. writing a  
> one-liner on the the command line), but I also wouldn’t care enough to  
> change it if I saw it in my codebase. It is totally irrelevant next to  
> concerns about object boundaries and mutability, so it seems to me that  
> any  
> disagreement is just bikeshedding.

The thing I don’t like about using .tap in the way sown is that it feels  
like a side effect, but admittedly that comes from my initial belief  
about  
the intention of .tap. As with most things, we find new uses for things  
that weren’t originally thought of, and I find that good for the most  
part.

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 23, 2013, 12:04pm UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/14 "2013-04-23T12:04:58Z")

</div>

tamouse mailing lists wrote in post #1106508:

> The thing I don’t like about using .tap in the way sown is that it feels  
> like a side effect, but admittedly that comes from my initial belief  
> about  
> the intention of .tap. As with most things, we find new uses for things  
> that weren’t originally thought of, and I find that good for the most  
> part.

I often find myself using idioms like

Klass.new.tap do  
…  
end

with Klass = Hash, Array, … , and dreaming about a shortcut to  
‘new.tap’ like ‘such\_that’, ‘with’, …

-md

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 27, 2013, 12:42pm UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/15 "2013-04-27T12:42:01Z")

</div>

Robert K. wrote in post #1106648:

> What is it that you want to achieve? Can you please come up with a more  
> realistic example?
> 
> Kind regards
> 
> robert

Hello Robert. I use this systematically, replacing for instance

def foo(args)  
hsh = Hash.new

## some lines building hsh by calling methods using args,

hsh  
end

by

def foo(args)  
Hash.new.tap do |hsh|  
## same code as above  
end  
end

I find the latter cleaner and easier to read, due to the extra  
indentation inside. I think Rails has a ‘returning’ method doing just  
that.

\_md

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 23, 2013, 4:53pm UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/16 "2013-04-23T16:53:31Z")

</div>

On Tue, Apr 23, 2013 at 12:05 PM, Michel D.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Klass.new.tap do  
> …  
> end

That doesn’t make any sense at all: there is no point in using #tap if  
you  
do not assign the result and not use the block parameter. (Note: #tap  
does  
not change self.) Or do you mean

Klass.new.instance\_eval do  
…  
end

> with Klass = Hash, Array, … , and dreaming about a shortcut to  
> ‘new.tap’ like ‘such\_that’, ‘with’, …

What is it that you want to achieve? Can you please come up with a more  
realistic example?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 27, 2013, 5:01pm UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/17 "2013-04-27T17:01:40Z")

</div>

Thank you very much for your various answers !

🙂

Why this kind of loop from 1 to 31 ? These magic numbers are the days in  
a month and I use them in a DaySelect class.

I decided to go with the most explicit implementation :

def options  
options = {}  
‘1’.upto(‘31’) do |day| options[day] = ‘%02d’ % day end  
options  
end

“tap”, “each\_with\_object” and so on are maybe overkill ? But  
being new to  
Ruby, this makes me realize that there is more than one way to do it and  
Ruby fits everybody’s style.

(By the way, sorry for my poor english, I’m a french-speaking guy.)

---

<div class="post-metadata">

**Author:** ![oria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@oria](https://www.ruby-forum.com/u/oria)\
**Post date:** [April 27, 2013, 7:24pm UTC](https://www.ruby-forum.com/t/tap-method-good-or-bad-practice/228410/18 "2013-04-27T19:24:27Z")

</div>

On Sat, Apr 27, 2013 at 12:42 PM, Michel D.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

I think Rails has a ‘returning’ method doing just

> that.

Yeah, Active Support had “returning” in the past, but was removed in  
Rails  
3 in favor of Object#tap.
