From: Harry Kakueki Date: 2009-08-29T07:21:17+09:00 Subject: Re: I need a string#all_indices method--is there such a thing? On Sat, Aug 29, 2009 at 12:05 AM, timr wrote: > On Aug 28, 5:02 am, Harry Kakueki wrote: >> >> class String >> def all_indices(reg) >> idx = [] >> (0...self.length).each{|x| idx << x if self[x..-1] =~ /\A#{reg}/} >> idx >> end >> end >> >> p "this is a test string for the ts in the worldt".all_indices(/th/) >> #> [0, 26, 36] >> p "banana".all_indices(/ana/) #> [1, 3] >> >> Harry >> > > This works and the code is more concise than what I had, but it is a > brute force approach that test for matches from every possible > substring. That would be a bit slow. > > This is not fast enough? class String def all_indices(reg) idx = [] (0...self.length).each{|x| idx << x if self[x..-1] =~ /\A#{reg}/} idx end end p ("this is a test string for the ts in the worldt"*1000).all_indices(/th/) I guess you are processing some big strings. Speed is not what you asked for. Well, until now :) Harry -- A Look into Japanese Ruby List in English http://www.kakueki.com/ruby/list.html