From: "David A. Black" Date: 2007-11-15T22:41:57+09:00 Subject: Re: alias_method :tap, :affect --1926193751-630496731-1195134120=:10624 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-630496731-1195134120=:10624" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-630496731-1195134120=:10624 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Thu, 15 Nov 2007, Pe=F1a, Botp wrote: > 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 met= hods).. > > naive example follows... > > ~> s > =3D> "This is a test" > > ~> s.capitalize!.capitalize! > NoMethodError: undefined method `capitalize!' for nil:NilClass > > breaks! > > ~> s.upcase!.upcase! > =3D> 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!) > =3D> "this is a test" > > long chain, no break :) > > btw, i like the #tap name, it's like tapping a chain so that it wont brea= k. (i'm not an english expert but that is how i usually use the word).. I am an English expert and I have no idea what "tap" is supposed to have to do with what this #tap method does. David --=20 Upcoming training by David A. Black/Ruby Power and Light, LLC: * Advancing With Rails, Berlin, Germany, November 19-22 * Intro to Rails, London, UK, December 3-6 (by Skills Matter) See http://www.rubypal.com for details! --1926193751-630496731-1195134120=:10624-- --1926193751-630496731-1195134120=:10624--