[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

counting in for .. in ..

Daniel Liebig

3/1/2008 10:25:00 PM

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

8 Answers

Gregory Seidman

3/1/2008 10:35:00 PM

0

On Sun, Mar 02, 2008 at 07:24:34AM +0900, Daniel Liebig 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


Phillip Gawlowski

3/1/2008 10:36:00 PM

0

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

Jano Svitok

3/1/2008 10:39:00 PM

0

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

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

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

Christopher Swasey

3/1/2008 10:43:00 PM

0

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 :)
>
> 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/Enume...


Christopher

7stud --

3/2/2008 5:40:00 AM

0

Gregory Seidman wrote:
> On Sun, Mar 02, 2008 at 07:24:34AM +0900, Daniel Liebig 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
--
Posted via http://www.ruby-....

William James

3/2/2008 5:46:00 AM

0

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

William James

3/2/2008 5:54:00 AM

0



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

Matthias Wächter

3/3/2008 11:19:00 AM

0

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