Re: increasing counter whithin loop?


#1

Hi Simon,

Hi Steve,

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

:confused:

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

actually these aren’t variables but values, symbols to be
specific:

http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_symbol.html

they aren’t used for any purpose in this example, the array just
had to contain something.

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

no they are not supplied by enumerator, each_cons yields arrays
do the block (containing 3 elements in this case).

because of the magick of ruby’s assign operator you can write

a, b, c = [1, 2, 3]

which is equal to

a, b, c = 1, 2, 3

or

a = 1; b = 2; c = 3

this is used here to give the 3 elements of the array yielded by
each_cons more meaningfull names.

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

an array. […] creates arrays, see

http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html#UA

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

because, well because I would have missed the first and the last
element of a otherwise. (I would have seen them in ‘prv’ or ‘nxt’
but never in ‘this’)

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.

just hang on

cheers

Simon