On Nov 16, 2007 12:40 PM, David A. Black <dblack / rubypal.com> wrote:

> It's interesting that almost every example of #tap ends up saying that
> it's probably better not to use it, for one reason or another (usually
> readability; sometimes the dangers of mixing bang and non-bang
> methods).
>
> It makes me wonder whether the main effect of #tap is going to be a
> lot of warnings that it isn't a best practice. Or are there examples
> that *don't* look worse than the alternatives?

def cleverly_return_an_array_after_building_it
  Array.new.tap do |ary|
    [1, 2, 3].each do |i|
      ary << i
    end
  end
end

instead of

def return_an_array_in_an_unclever_fashion_after_building_it
  ary = []
  [1, 2, 3].each do |i|
    ary << i
  end
  ary
end

(Yeah, that's a horrible example, but you the point is valid.  If only
for this reason, I want to see #tap in Ruby.  It's probably my
favorite addition to the language, so far (keyword arguments would be,
had they been added).)