From: Harry Kakueki Date: 2009-08-28T21:02:32+09:00 Subject: Re: I need a string#all_indices method--is there such a thing? > Does this do what you want? > > class String > def all_indices(reg) > tmp,idx = [],[] > (0...self.length).each{|x| tmp[x] = self[x..-1]} > tmp.each_with_index{|y,i| idx << i if y =~ /\A#{reg}/} > idx > end > end > > p "this is a test string for the ts in the worldt".all_indices(/th/) > #> [0, 26, 36] > > > Harry > Sorry, it looks like I had an unnecessary line in there. 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 -- A Look into Japanese Ruby List in English http://www.kakueki.com/ruby/list.html