# Re: increasing counter whithin loop?

**URL:** <https://www.ruby-forum.com/t/re-increasing-counter-whithin-loop/96021>\
**Category:** Ruby\
**Created:** [December 8, 2005, 11:50am UTC](https://www.ruby-forum.com/t/re-increasing-counter-whithin-loop/96021 "2005-12-08T11:50:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon.kroeger.ext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@simon.kroeger.ext](https://www.ruby-forum.com/u/simon.kroeger.ext)\
**Post date:** [December 8, 2005, 11:50am UTC](https://www.ruby-forum.com/t/re-increasing-counter-whithin-loop/96021/1 "2005-12-08T11:50:13Z")

</div>

> > able to use it. I’d probably have to create each\_with\_prev\_and\_next.

It’s allready there:

* * *

require ‘enumerator’  
a=[:a, :b, :b, :c, :b, :d, :e, :e]

## ([nil] + a + [nil]).each\_cons(3) do |prv, this, nxt| next if prv == this print ‘double\_’ if this == nxt puts this end

output:  
a  
double\_b  
c  
b  
d  
double\_e

cheers

Simon

---

<div class="post-metadata">

**Author:** ![simon.kroeger.ext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@simon.kroeger.ext](https://www.ruby-forum.com/u/simon.kroeger.ext)\
**Post date:** [December 8, 2005, 1:08pm UTC](https://www.ruby-forum.com/t/re-increasing-counter-whithin-loop/96021/2 "2005-12-08T13:08:37Z")

</div>

Hi,

> > > able to use it. I’d probably have to create each\_with\_prev\_and\_next.
> 
> It’s allready there:

[…]

> require ‘enumerator’  
> ([nil] + a + [nil]).each\_cons(3) do |prv, this, nxt|

…

That’s interesting, thanks.

Patrick

---

<div class="post-metadata">

**Author:** ![simon.kroeger.ext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@simon.kroeger.ext](https://www.ruby-forum.com/u/simon.kroeger.ext)\
**Post date:** [December 8, 2005, 1:45pm UTC](https://www.ruby-forum.com/t/re-increasing-counter-whithin-loop/96021/3 "2005-12-08T13:45:22Z")

</div>

On Thursday 08 December 2005 05:47 am, Kroeger, Simon (ext) wrote:

> print ‘double\_’ if this == nxt
> 
> cheers
> 
> Simon

Hi Simon,

I just ran your code, and got the exact output you got, but I don’t  
understand  
a bit of it.

First, I’m unclear as to the use of variables starting with colon. What  
are  
they, when are they used?

Also, are prv, this and nxt just any old variables, or are they somehow  
supplied by enumerator?

Is variable a a string, an array, or something else?

Why did you place an array containing only nil before and after a in the  
parentheses?

Thank you for this code. When I asked my question about how to stop  
writing  
Ruby with a Perl accent, I was hoping to understand constructs like this  
one.  
I have about 8 days experience with Ruby and so have some catching up to  
do.

Thanks

SteveT

Steve L.

> **[Troubleshooters.Com](http://www.troubleshooters.com)**
>
> The process and mindset of troubleshooting.

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![simon.kroeger.ext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@simon.kroeger.ext](https://www.ruby-forum.com/u/simon.kroeger.ext)\
**Post date:** [December 8, 2005, 4:17pm UTC](https://www.ruby-forum.com/t/re-increasing-counter-whithin-loop/96021/4 "2005-12-08T16:17:44Z")

</div>

On Dec 8, 2005, at 6:41 AM, Steve L. wrote:

> I just ran your code, and got the exact output you got, but I don’t  
> understand  
> a bit of it.

One interesting technique when you are struggling, is to ask Ruby  
your questions directly. Let’s try it!

> First, I’m unclear as to the use of variables starting with colon.  
> What are  
> they, when are they used?

> > :what\_am\_i.class  
> > =\> Symbol

Ruby says those are Symbols. We can even ask what they can do:

> > :what\_do\_i\_do.methods - Object.new.methods  
> > =\> [“id2name”, “to\_sym”, “to\_i”, “to\_int”]

Wow, not much, huh? We could then look up the class and methods in  
the documentation for more details.

Symbols are just immutable Strings in Ruby. They generally have good  
performance (because they can’t change) and can be easy on memory  
(because every time you ask for :obj, you get the exact same  
object). They are never garbage collected though, so don’t generate  
a huge number of them.

> Also, are prv, this and nxt just any old variables, or are they  
> somehow  
> supplied by enumerator?

> > (1…10).each\_cons(4) do |one, two, three, four|  
> > ?\> p [one, two, three, four]
> 
> > end  
> > [1, 2, 3, 4]  
> > [2, 3, 4, 5]  
> > [3, 4, 5, 6]  
> > [4, 5, 6, 7]  
> > [5, 6, 7, 8]  
> > [6, 7, 8, 9]  
> > [7, 8, 9, 10]  
> > =\> nil

Looks like just variables, eh?

each\_cons() yields ranges of "cons"ecutive members. In the example  
you mentioned, this is used to simulate next and previous objects.

> Is variable a a string, an array, or something else?

> > a=[:a, :b, :b, :c, :b, :d, :e, :e]  
> > =\> [:a, :b, :b, :c, :b, :d, :e, :e]
> 
> > a.class  
> > =\> Array
> 
> > a.map { |e| e.class }  
> > =\> [Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol]

Ruby says it is an Array of Symbols.

> Why did you place an array containing only nil before and after a  
> in the  
> parentheses?

> > [nil] + a + [nil]  
> > =\> [nil, :a, :b, :b, :c, :b, :d, :e, :e, nil]

That just puts two new items in the Array. Hmm, so if we combine  
that with the iterator:

> > ([nil] + a + [nil]).each\_cons(3) do |prv, this, nxt|  
> > ?\> p [prv, this, nxt]
> 
> > end  
> > [nil, :a, :b]  
> > [:a, :b, :b]  
> > [:b, :b, :c]  
> > [:b, :c, :b]  
> > [:c, :b, :d]  
> > [:b, :d, :e]  
> > [:d, :e, :e]  
> > [:e, :e, nil]  
> > =\> nil

Ah, now I see it. It’s used to add a nil object to prv and nxt at  
the ends of iteration.

> Thank you for this code. When I asked my question about how to stop  
> writing  
> Ruby with a Perl accent, I was hoping to understand constructs like  
> this one.  
> I have about 8 days experience with Ruby and so have some catching  
> up to do.

Welcome to Ruby!

James Edward G. II
