From: "Peña, Botp" Date: 2007-11-15T22:27:00+09:00 Subject: Re: alias_method :tap, :affect From: furtive # On Nov 12, 2:31 pm, Martin # > #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. # So, to throw this out again --- there should also be Object#as which # returns the block result (I like the name 'as' but I am not # particularly attached to it). I can personally attest to its # usefulness, and I can show reams of examples in addition to the above # one I gave. (my previous message seems to have lost, so i apologize for a 2nd post) tap is indispensable for me when it comes to chaining (specially bang methods).. naive example follows... ~> s => "This is a test" ~> s.capitalize!.capitalize! NoMethodError: undefined method `capitalize!' for nil:NilClass breaks! ~> s.upcase!.upcase! => nil breaks! ~> s.upcase!.capitalize! NoMethodError: undefined method `capitalize!' for nil:NilClass breaks! ~> s.tap(&:upcase!).tap(&:upcase!).tap(&:capitalize!).tap(&:capita lize!).tap(&:downcase!).tap(&:downcase!) => "this is a test" long chain, no break :) btw, i like the #tap name, it's like tapping a chain so that it wont break. (i'm not an english expert but that is how i usually use the word).. kind regards -botp