I need a string#all_indices method--is there such a thing?

Hi –

On Fri, 28 Aug 2009, Harry K. wrote:

class String
def all_indices(reg)
idx = []
(0…self.length).each{|x| idx << x if self[x…-1] =~ /\A#{reg}/}
idx
end
end

Might as well let #select do the choosing:

def all_indices(re)
(0…size).select {|i| self[i…-1][/\A#{re}/] }
end

And maybe better to create the regex only one:

def all_indices(re)
re = /\A#{re}/
(0…size).select {|i| self[i…-1][re] }
end

David


David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
Ruby/Rails training, mentoring, consulting, code-review
Latest book: The Well-Grounded Rubyist (The Well-Grounded Rubyist)

September Ruby training in NJ has been POSTPONED. Details to follow.

Hi,

Am Samstag, 29. Aug 2009, 11:38:19 +0900 schrieb 7rans:

On Aug 28, 4:25 am, timr [email protected] wrote:

A lot of solutions have been given here. It would be nice to see a
test/benchmark matrix to compare them, if anyone is up to it.

Sure I agree. But my solution was just to show some aspect of
String#scan, not of any practical sense.

Bertram