From: gga Date: 2006-08-13T07:50:13+09:00 Subject: Re: ruby equiv of perl pos > Another idea: > > string = "one\0two\0threeeeeee\0" > result = '' > string.scan(/\0/) do > result << '\%o' % $`.length > end This is simpler to write, but will be slower on longer strings. #!/usr/bin/env ruby @string = "\0\0\0asda\0\0\0sdasd" * 5000 def test_a result = '' pos = 0 while pos = @string.index( /\0/, pos ) match = Regexp.last_match pos += match.size result << '\%o' % (pos - 1) end return result end def test_b result = '' @string.scan(/\0/) do result << '\%o' % $`.length end return result end require 'benchmark' Benchmark.benchmark() { |x| x.report('a:') { test_a } x.report('b:') { test_b } }