Ruby_indices
I thought Ruby Strings also needed the Raku indices
method.
class String
def indices(m, overlap: false)
[].tap {|ar|
overlap ?
( self.scan(/(?=(#{m.to_s}))/) { ar << $~.offset(0)[0] } ) :
( self.scan(m) { ar << $~.offset(0)[0] } )
}
end
end
p 'banana'.indices 'a' # [1, 3, 5]
p 'banana'.indices /a/ # [1, 3, 5]
p 'dadad'.indices 'dad', overlap: true # [0, 2]
p 'dadad'.indices /dad/, overlap: true # [0, 2]
It returns an Array of all indexes where matching occurred.
Also do overlapping matches.. with overlap: true
.
So 'dadad'.indices /dad/, overlap: true
will find the index of both DADad
and daDAD
.