counting in for .. in ..

D

Daniel Liebig

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

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

Gregory Seidman

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
 
P

Phillip Gawlowski

Daniel said:
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 :)

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

[..]

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
 
J

Jano Svitok

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

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

Christopher Swasey

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

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.

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
 
7

7stud --

Gregory said:
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
 
W

William James

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

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.


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"]
 
W

William James

William said:
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 :)

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.


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

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
 
M

Matthias Wächter

Jano said:
Let's start with equivalent code:

@several_things.each do |thing|
print thing
end

I'm really puzzled why Rails doesn't use the Ruby idiom for the templates ...

- Matthias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,108
Latest member
AmeliaAmad

Latest Threads

Top