From: Robert Klemme Date: 2004-09-28T16:29:24+09:00 Subject: Re: keep changes in string outside of scope? "David A. Black" schrieb im Newsbeitrag news:Pine.LNX.4.44.0409271452500.8281-100000@wobblini... > Hi -- > > On Tue, 28 Sep 2004, David A. Black wrote: > > > thestring.sub!(/\d{5}/) {|x| "#{x},"} > > Amendment: as James G. suggested, use gsub! if you have more than one > in one string :-) Also make sure that you have no street numbers with > five digits in a row. The block variant isn't necessary in this case. A remark about efficiency: require 'benchmark' REP = 1000 thestring = ("foo 12345 bar baz" * 100).freeze Benchmark.bm(15) do |b| b.report "direct" do REP.times { thestring.gsub(/\d{5}/, ',\\&') } end b.report "direct group" do REP.times { thestring.gsub(/(\d{5})/, ',\\1') } end b.report "block" do REP.times { thestring.gsub(/\d{5}/) {|m| ",#{m}"} } end end user system total real direct 0.516000 0.000000 0.516000 ( 0.527000) direct group 0.546000 0.000000 0.546000 ( 0.537000) block 1.829000 0.000000 1.829000 ( 1.844000) Kind regards robert