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.

路路路

--
Posted via http://www.ruby-forum.com/.

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

路路路

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

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

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

[..]

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

路路路

--
Phillip Gawlowski

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 Sat, Mar 1, 2008 at 11:24 PM, Daniel Liebig <d.liebig@wevin.de> wrote:

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.
--
Posted via http://www.ruby-forum.com/.

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 3/1/08, Daniel Liebig <d.liebig@wevin.de> 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

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.

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

路路路

On Mar 1, 4:24 pm, Daniel Liebig <d.lie...@wevin.de> 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

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.

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

路路路

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

--
Posted via http://www.ruby-forum.com/.

William James wrote:

路路路

On Mar 1, 4:24 pm, Daniel Liebig <d.lie...@wevin.de> 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
>
> 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.
>

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

Jano Svitok schrieb:

路路路

On Sat, Mar 1, 2008 at 11:24 PM, Daniel Liebig <d.liebig@wevin.de> 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

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

- Matthias