From: Eric Hodel Date: 2006-11-16T06:49:30+09:00 Subject: Re: #returning and #tap On Nov 8, 2006, at 6:40 AM, Trans wrote: > Had use for this today: #returning is a convenience method you'll find > in both Facets and ActiveSupport. Jamis' blogged it the other day: > > http://weblog.jamisbuck.org/2006/10/27/mining-activesupport-object- > returning > > Any one else think this is worthy of standard Ruby? From the blog post: >> The latter is no shorter than the former, but it reads more >> elegantly. It feels more �Rubyish�. And there are times that it >> can save you a few lines of code, if that�s important. (Just scan >> the Rails source code for more examples.) So... it usually doesn't save any lines, it adds typing, it hides the object you're returning in the middle of a statement and it is significantly more expensive to execute[1]. (Oh, but maybe, in some cases, it might save you one or two lines of code.) No thanks. I don't think that makes it more rubyish. Every time I've encountered #returning in Rails I've found it decreased the readability of the code and uselessly increased the complexity. (Wait, now what does this return? oh, yeah, way up there.) 1: Rehearsal --------------------------------------------- empty 0.290000 0.000000 0.290000 ( 0.391447) returning 5.120000 0.050000 5.170000 ( 5.467861) regular 2.660000 0.020000 2.680000 ( 2.900946) ------------------------------------ total: 8.140000sec user system total real empty 0.300000 0.000000 0.300000 ( 0.316623) returning 5.160000 0.050000 5.210000 ( 5.815804) regular 2.650000 0.020000 2.670000 ( 2.824744) require 'benchmark' def returning(value) yield(value) value end N = 1_000_000 Benchmark.bmbm do |bm| bm.report 'empty' do N.times do end end bm.report 'returning' do N.times do returning(Object.new) do |o| o.nil? end end end bm.report 'regular' do N.times do o = Object.new; o.nil?; o end end end -- Eric Hodel - drbrain@segment7.net - http://blog.segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com