From: "Peña, Botp" Date: 2007-11-15T22:33:18+09:00 Subject: Re: alias_method :tap, :affect From: furtive.clown@gmail.com [mailto:furtive.clown@gmail.com] # On Nov 12, 2:31 pm, Martin DeMello wrote: # > On Nov 12, 2007 11:10 AM, wrote: # > > This is the first I've heard of Object#tap. It seems # backward because # > > you almost always want the result of the block, not the # object back # > > again. # > #tap is the opposite use case - you want to "tap" the object stream, # > observing without affecting. # # 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 => "THIS IS A TEST" ~> s.upcase!.capitalize! NoMethodError: undefined method `capitalize!' for nil:NilClass ... ~> s.tap{|x| x.upcase!}.capitalize! => "This is a test" ~> s => "This is a test" C:\ruby1.9\bin>ri Object#tap -T ------------------------------------------------------------- Object#tap obj.tap{|x|...} => 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;..