From: furtive.clown@... Date: 2007-11-13T07:20:00+09:00 Subject: Re: alias_method :tap, :affect On Nov 12, 4:37 pm, James Edward Gray II wrote: > On Nov 12, 2007, at 3:25 PM, furtive.cl...@gmail.com wrote: > > > > > On Nov 12, 3:55 pm, James Edward Gray II > > wrote: > >> On Nov 12, 2007, at 2:40 PM, furtive.cl...@gmail.com wrote: > > >> I guess I want to see some more examples, because I'm seriously > >> doubting the usefulness. The example you showed so far was using it > >> to create this: > > >> platform_audio_files = audio_stems.map { |f| > >> f + "." + audio_ext[platform] > >> }.as { |t| > >> t + audio_basenames > >> }.map { |f| > >> File.join(audio_dir, f) > >> } > > >> instead of: > > >> platform_audio_files = (audio_stems.map { |f| > >> f + "." + audio_ext[platform] > >> } + audio_basenames).map { |f| > >> File.join(audio_dir, f) > >> } > > >> I don't really understand what the problem is with the second version > >> that's shorter and easier for me to understand. If it's the need to > >> introduce some parentheses into the call chain, we can fix that: > > >> platform_audio_files = audio_stems.map { |f| > >> f + "." + audio_ext[platform] > >> }.concat(audio_basenames).map { |f| > >> File.join(audio_dir, f) > >> } > > >> So yes, please send more examples as I remain unconvinced. > > > It was only a coincidence that you found an Array method which > > corresponded to the operation in the block, namely Array#concat. Your > > example breaks down once the operation is nontrivial --- something > > more complex than '+'. > > > We can also introduce temporaries inside the block without causing > > distraction in the outer scope. The purpose of the block chain is to > > produce a value for platform_audio_files; a flood temporary variables > > on the same scope obscures this purpose. > > My counter argument would be that, if it becomes non-trivial, you're > trying to be too fancy doing it all in one call chain. Split it up > into the few lines of code it needs to be. Then, if the variables > and such become significant enough to affect the outer scope, it's > time to tuck the operation away in some method where it belongs. If you had only one temporary variable in the block, would you move it to a method? I would prefer to keep that temporary inside the #as block -- it truly improves understandably and readability. There is no confusion about what the intent is: we are interested in the result, not the temporaries. #as is also used to make function calls prettier: filename = File.basename(input.map { |t| t.gsub(re, "_") }.join) filename = input.map { |t| t.gsub(re, "_") }.join.as { |t| File.basename(t) } I greatly prefer the latter. I want to chain, chain, chain, and I don't want pesky prefix-y function calls like File.basename() to cramp my style.