Increasing counter whithin loop?

> 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

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

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 Litt

slitt@troubleshooters.com

···

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

> > 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

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 Gray II

···

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