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 17:38
(Received via mailing list)
> 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.


> 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

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


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

> 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


This topic is locked and can not be replied to.