Forum: Ruby Re: increasing counter whithin loop?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
87e9a89c53ccf984db792113471c2171?d=identicon&s=25 simon.kroeger.ext (Guest)
on 2005-12-08 10: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...

cheers

Simon
This topic is locked and can not be replied to.