# Increasing counter whithin loop?

**URL:** <https://rubytalk.org/t/increasing-counter-whithin-loop/23109>\
**Category:** ruby-talk\
**Created:** [8 December 2005 09:45 UTC](https://rubytalk.org/t/increasing-counter-whithin-loop/23109 "2005-12-08T09:45:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kroeger\_Simon\_ext](https://avatars.discourse-cdn.com/v4/letter/k/5daacb/32.png) [@Kroeger\_Simon\_ext](https://rubytalk.org/u/Kroeger_Simon_ext)\
**Post date:** [8 December 2005 09:45 UTC](https://rubytalk.org/t/increasing-counter-whithin-loop/23109/1 "2005-12-08T09:45:27Z")

</div>

> [...]  
> \> A lot of Ruby's clarity comes not from being able to write things  
> \> clearly in the "raw" language, but from being able to easily  
> \> customize the language for your needs.
> 
> Of course, and that is what I am asking in the whole thread! Is there  
> a nicer way for this loop... Let's see. I have this each\_with\_next  
> now, but still I am completely unstatisfied with my loop. But I am now  
> convinced that I have the optimal solution, even if it the way it  
> looks now. And btw., I need a look-ahead for this specific purpose.  
> The each\_with\_previous was a very nice hint, thanks, even if I wasn't  
> able to use it. I'd probably have to create each\_with\_prev\_and\_next.

Hmm, I still don't know exactly what you realy want/need. If you just  
want to replace two instances of :b with double\_b, have a look at this:

> **···**
>
> -----------------------------------------------------------------  
> require 'enumerator'  
> a=[:a, :b, :b, :c, :b, :d, :e]
> 
> a[a.to\_enum(:each\_cons, 2).to\_a.index([:b, :b]), 2] = 'double\_b'  
> puts a  
> -----------------------------------------------------------------
> 
> (yes, of course this is completely without error checking and  
> doesn't work for more than one occurrence of :b, :b)
> 
> As I said, I'm not realy sure that's what you are looking for...
> 
> cheers
> 
> Simon
