From: Trans Date: 2007-11-16T22:23:22+09:00 Subject: Re: wondering about #tap (was: Re: Thoughts about Array#compact!, Array#flatten!, Array#reject!, String#strip!, String#capitalize!, String#gsub!, etc.) On Nov 16, 6:40 am, "David A. Black" wrote: > Hi -- > > On Fri, 16 Nov 2007, Michael Neumann wrote: > > If you use Ruby 1.9, you can use Object#tap: > > > "I am a 20MB string".tap {|i| i.downcase!}.split(/\s+/).tap {|i| > > i.reject!{|s|s.length>20} > > > But why not split it in multiple lines? I think it will become more readable > > than using #tap: > > > str = "..." > > str.downcase! > > arr = str.split(..) > > arr.reject!{...} > > 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? It possible to define tap slightly differently by returning a functor if no block is given, then str.tap.downcase!.split ... would work. T.