From: Trans Date: 2007-01-03T12:43:20+09:00 Subject: Re: Mini-RCR: Extra Argument for Array#join Ryan Davis wrote: > On Jan 1, 2007, at 8:40 AM, Trans wrote: > > > I wonder how this would fair. > > > > class Array > > alias :old_join :join > > def join( sep=$,, last_sep=nil ) > > s = old_join(sep) > > if last_sep > > rsep = Regexp.escape(sep.to_s) > > rlast = Regexp.escape(last.to_s) > > s.sub!(/#{rsep}#{rlast}$/,"#{last_sep}#{last}") > > end > > return s > > end > > end > > > > Sorry, the bencmark script wasn't posted and I didn't feel like > > recreating it. > > % ./blah.rb 1_000_000 > # of iterations = 1000000 > user system total real > null_time 0.140000 0.000000 0.140000 ( 0.139480) > ryan 19.760000 0.020000 19.780000 ( 19.810269) > trans 21.290000 0.040000 21.330000 ( 21.398592) > Loaded suite ./blah > Started > .... > Finished in 0.001109 seconds. > > 4 tests, 24 assertions, 0 failures, 0 errors Eek. That was worse then I thought it would be. Thanks for showing me though, Ryan. Of course now that I'm looking at it again I'm wondering how we missed: class Array def join( sep=$,, last_sep=nil ) return old_join(sep) unless last_sep [slice(0...-1).old_join(sep), last].old_join(last_sep) end end That must to be faster. T.