Forum: Ruby Re: increasing counter whithin loop?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
simon.kroeger.ext (Guest)
on 2005-12-08 11:48
(Received via mailing list)
> 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...


This topic is locked and can not be replied to.