Counting in for .. in

Hi,

i’m doing my first steps in ruby (on rails) and often use things like

for thing in @several_things
print thing
end

Ok so far, looks great :slight_smile:

But i often need a counter inside of the loop, may it be to create row
colors or other stuff. What i end up with then is:

i = 0
for thing in @several_things
print thing + " No. " + i.to_s
i += 1
end

Pardon me, but this is ugly, compared to the first clean lines. So far i
know ruby as an elegant language, so i wouldn’t be surpised if there’d
be any kind of implemented counter in the for loop or a smarter way to
implement it.

Any hints?

Thanks a lot for any help!
R.D.

On Sun, Mar 02, 2008 at 07:24:34AM +0900, Daniel L. wrote:

i’m doing my first steps in ruby (on rails) and often use things like

for thing in @several_things
print thing
end
[…]

For reasons I find it difficult to express the for-in construct is
frowned
upon. There are better and more flexible iteration constructs, and it
looks
like you are starting to feel the need for them.

But i often need a counter inside of the loop, may it be to create row
colors or other stuff. What i end up with then is:

i = 0
for thing in @several_things
print thing + " No. " + i.to_s
i += 1
end
[…]

This is where each_with_index, one of those more flexible iteration
constructs, comes in.

@several_thing.each_with_index do |thing,i|
print thing + " No. " + i.to_s
i += 1
end

You should be familiar with ri for reading Ruby API documentation. Look
up
Enumerable for the wide variety of iteration/enumeration constructs Ruby
gives you right out of the box. There are even more advanced constructs
available from other libraries (e.g. facets).

Thanks a lot for any help!
R.D.
–Greg

Daniel L. wrote:

But i often need a counter inside of the loop, may it be to create row
Any hints?

Thanks a lot for any help!
R.D.

C:\Documents and Settings\CynicalRyan>cat test.rb
@things = [“one”, “two”, “three”]

@things.each_with_index do |thing,i|
puts thing
puts i
end
C:\Documents and Settings\CynicalRyan>ruby -v test.rb
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
one
0
two
1
three
2

On Sat, Mar 1, 2008 at 11:24 PM, Daniel L. [email protected]
wrote:

Hi,

i’m doing my first steps in ruby (on rails) and often use things like

for thing in @several_things
print thing
end

Let’s start with equivalent code:

@several_things.each do |thing|
print thing
end

Ok so far, looks great :slight_smile:

But i often need a counter inside of the loop, may it be to create row
colors or other stuff. What i end up with then is:

i = 0
for thing in @several_things
print thing + " No. " + i.to_s
i += 1
end

Now let me introduce Enumerable module, that contains each_with_index:

@several_things.each_with_index do |thing, i|
print “#{thing} No. #{i}”
end

Note: #{expression} is replaced with expression.to_s (more or less)
Lookup Enumerable in the docs, it contains lots of useful stuff.

On 3/1/08, Daniel L. [email protected] wrote:

But i often need a counter inside of the loop, may it be to create row
be any kind of implemented counter in the for loop or a smarter way to
implement it.

Simple. Use #each_with_index instead of for… in:

@several_things.each_with_index do |thing, index|

end

Read up on Enumerable. It’s pretty handy:
http://www.ruby-doc.org/core/classes/Enumerable.html

Christopher

On Mar 1, 4:24 pm, Daniel L. [email protected] wrote:

But i often need a counter inside of the loop, may it be to create row
be any kind of implemented counter in the for loop or a smarter way to
implement it.

E:>irb --prompt xmp
a = %w(zero one two three)
==>[“zero”, “one”, “two”, “three”]
a.each_with_index{|x,i| puts “Item no. #{i} is #{x}.”}
Item no. 0 is zero.
Item no. 1 is one.
Item no. 2 is two.
Item no. 3 is three.
==>[“zero”, “one”, “two”, “three”]

Gregory S. wrote:

On Sun, Mar 02, 2008 at 07:24:34AM +0900, Daniel L. wrote:

i’m doing my first steps in ruby (on rails) and often use things like

for thing in @several_things
print thing
end
[…]

For reasons I find it difficult to express the for-in construct is
frowned
upon. There are better and more flexible iteration constructs, and it
looks
like you are starting to feel the need for them.

But i often need a counter inside of the loop, may it be to create row
colors or other stuff. What i end up with then is:

i = 0
for thing in @several_things
print thing + " No. " + i.to_s
i += 1
end
[…]

This is where each_with_index, one of those more flexible iteration
constructs, comes in.

@several_thing.each_with_index do |thing,i|
print thing + " No. " + i.to_s
i += 1
end

Rather that should be:

arr = [‘a’, ‘b’, ‘c’]

arr.each_with_index do |elmt, i|
puts “Element at index #{i} is: #{elmt}”
end

–output:–
Element at index 0 is: a
Element at index 1 is: b
Element at index 2 is: c

William J. wrote:

know ruby as an elegant language, so i wouldn’t be surpised if there’d
Item no. 1 is one.
Item no. 2 is two.
Item no. 3 is three.
==>[“zero”, “one”, “two”, “three”]

puts a.zip((0…a.size).to_a).map{|a| “Item #{a[1]} is #{a[0]}”}
Item 0 is zero
Item 1 is one
Item 2 is two
Item 3 is three

Jano S. schrieb:

@several_things.each do |thing|
print thing
end

I’m really puzzled why Rails doesn’t use the Ruby idiom for the
templates …

  • Matthias