2009/8/28 Robert D. [email protected]:
the recent thread about all_indicies has somehow made me think that I
need a more general inject.
Here is my first shot on it:
http://pastie.org/597659
I didn’t know StopIteration yet. That’s nice.
Your use case is a bit unclear to me. Also, I do not see why you need
to pass variables via the block. Somehow that looks suspiciously more
complex to me than it could be. A few ideas
class String
def indices rgx, idx = 0
r = []
idx = index rgx, idx
while idx
r << idx
idx = index rgx, idx.succ
end
r
end
def indices2 rgx, idx = 0
idx = index rgx, idx
while idx
yield idx
idx = index rgx, idx.succ
end
self
end
def indices3 rgx, idx = 0
scan rgx do
i = $`.length
yield i if i >= idx
end
end
def indices4 rgx, idx = 0
to_enum(:scan, rgx).inject [] do |r,|
i = $~.offset(0).first
yield i if i >= idx
end
end
end
irb(main):154:0* “abcabc”.indices(“a”,1)
=> [3]
irb(main):155:0> “abcabc”.indices2(“a”,1) {|i| p i}
“a3
bcabc”.indices3(“a”,1) {|i| p i}
=> “abcabc”
irb(main):156:0> “abcabc”.indices3(“a”,1) {|i| p i}
“abcabc”.indices4(“a”,1) {|i| p i}
3
=> “abcabc”
irb(main):157:0> “abcabc”.indices4(“a”,1) {|i| p i}
3
=> 3
irb(main):158:0>
Ok, I admit, I got carried away. 
But how to call that beast, #inject_with is somehow not really what I like.
Actually I think #inject would be a nice name, but it is already taken.
Only few come to mind: roll, rotate, circulate
Kind regards
robert