From: furtive.clown / gmail.com [mailto:furtive.clown / gmail.com]=20
# On Nov 12, 2:31 pm, Martin DeMello <martindeme... / gmail.com> wrote:
# > On Nov 12, 2007 11:10 AM,  <furtive.cl... / gmail.com> wrote:
# > > This is the first I've heard of Object#tap.  It seems=20
# backward because
# > > you almost always want the result of the block, not the=20
# object back
# > > again.
# > #tap is the opposite use case - you want to "tap" the object stream,
# > observing without affecting.
#=20
# OK I understand Object#tap now.  At first I thought the motivation was
# to modify the object inside the block, but now I see that it can be a
# useful part of functional-style ruby.

~> s
=3D> "THIS IS A TEST"
~> s.upcase!.capitalize!
NoMethodError: undefined method `capitalize!' for nil:NilClass
...
~> s.tap{|x| x.upcase!}.capitalize!
=3D> "This is a test"
~> s
=3D> "This is a test"

C:\ruby1.9\bin>ri Object#tap -T
------------------------------------------------------------- Object#tap
     obj.tap{|x|...}    =3D> obj
------------------------------------------------------------------------
     Returns the receiver after executing the block given. Its main
     purpose is to be inserted in the method chain.

kind regards -botp
The essence of knowledge, is having it, to apply it;..