[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

for vs. each

Fear Dubh

2/27/2005 11:23:00 AM

Hello,

Is there a difference between:
for foo in bar do ...
and
bar.each do |foo| ...

or is it just a question of style?
I prefer the look of "for ... do ...",

but I mostly see each in the samples posted by the Ruby
wizards on the NG.

Thanks


15 Answers

ts

2/27/2005 11:28:00 AM

0

>>>>> "F" == Fear Dubh <feardubh@spam.spam> writes:

F> Is there a difference between:
F> for foo in bar do ...
F> and
F> bar.each do |foo| ...

uln% ruby -e 'for a in [12] do end; p a'
12
uln%

uln% ruby -e '[12].each do |a| end; p a'
-e:1: undefined local variable or method `a' for main:Object (NameError)
uln%


Guy Decoux



Christian Neukirchen

2/27/2005 12:47:00 PM

0

"Fear Dubh" <feardubh@spam.spam> writes:

> Hello,
>
> Is there a difference between:
> for foo in bar do ...
> and
> bar.each do |foo| ...
>
> or is it just a question of style?
> I prefer the look of "for ... do ...",
>
> but I mostly see each in the samples posted by the Ruby
> wizards on the NG.
>
> Thanks

I strongly prefer #each over for, because it is more consistent and
doesn't hide the fact that there really is a method call to #each.

I think I could agree to use for in rake and ERB (though I don't use
ERB at all), because it may have "cleaner" look for a DSL. For
standard Ruby code, use #each just like everyone else :-).

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


Fear Dubh

2/27/2005 2:27:00 PM

0


"ts" <decoux@moulon.inra.fr> wrote in message
news:200502271128.j1RBSA019680@moulon.inra.fr...
> >>>>> "F" == Fear Dubh <feardubh@spam.spam> writes:
>
> F> Is there a difference between:
> F> for foo in bar do ...
> F> and
> F> bar.each do |foo| ...
>
> uln% ruby -e 'for a in [12] do end; p a'
> 12
> uln%
>
> uln% ruby -e '[12].each do |a| end; p a'
> -e:1: undefined local variable or method `a' for main:Object (NameError)
> uln%
>
>
Thanks!

Does each have a fatter frame then?
Is "for ... in ... do ..." more efficient?

> Guy Decoux
>
>
>

Regards

--Fear Dubh


Robert Klemme

2/27/2005 4:19:00 PM

0


"Fear Dubh" <feardubh@spam.spam> schrieb im Newsbeitrag
news:cvslbj$9ns$1@reader01.news.esat.net...
>
> "ts" <decoux@moulon.inra.fr> wrote in message
> news:200502271128.j1RBSA019680@moulon.inra.fr...
>> >>>>> "F" == Fear Dubh <feardubh@spam.spam> writes:
>>
>> F> Is there a difference between:
>> F> for foo in bar do ...
>> F> and
>> F> bar.each do |foo| ...
>>
>> uln% ruby -e 'for a in [12] do end; p a'
>> 12
>> uln%
>>
>> uln% ruby -e '[12].each do |a| end; p a'
>> -e:1: undefined local variable or method `a' for main:Object (NameError)
>> uln%
>>
>>
> Thanks!
>
> Does each have a fatter frame then?
> Is "for ... in ... do ..." more efficient?

I would believe not, but you can test this yourself (hint "ruby -r profile"
and module Benchmark).

Indenpendently of that I strongly recommend the usage of "each" as it is
used 99% of the cases and it's simply standard. Although it's not an
enforced convention, people will expect that and it makes everybody's life
easier to stick to some conventions.

Kind regards

robert

Fear Dubh

2/27/2005 7:52:00 PM

0

Hi,

"Robert Klemme" <bob.news@gmx.net> wrote in message
news:38ea7eF5o769pU1@individual.net...
>
> "Fear Dubh" <feardubh@spam.spam> schrieb im Newsbeitrag
> news:cvslbj$9ns$1@reader01.news.esat.net...
> >
> > "ts" <decoux@moulon.inra.fr> wrote in message
> > news:200502271128.j1RBSA019680@moulon.inra.fr...
> >> >>>>> "F" == Fear Dubh <feardubh@spam.spam> writes:
> >>
> >> F> Is there a difference between:
> >> F> for foo in bar do ...
> >> F> and
> >> F> bar.each do |foo| ...
> >>
> >> uln% ruby -e 'for a in [12] do end; p a'
> >> 12
> >> uln%
> >>
> >> uln% ruby -e '[12].each do |a| end; p a'
> >> -e:1: undefined local variable or method `a' for main:Object
(NameError)
> >> uln%
> >>
> >>
> > Thanks!
> >
> > Does each have a fatter frame then?
> > Is "for ... in ... do ..." more efficient?
>
> I would believe not, but you can test this yourself (hint "ruby -r
profile"
> and module Benchmark).
>
> Indenpendently of that I strongly recommend the usage of "each" as it is
> used 99% of the cases and it's simply standard. Although it's not an
> enforced convention, people will expect that and it makes everybody's life
> easier to stick to some conventions.
>
OK, Thanks,

Now I feel like a bold child, scolded for stuffing my face
with this delicious syntax sugar! :-)

I have a further question:
As ts showed "for ... in ... do" is not exactly the same as "... each do
....",
so is there an "un-sugared" expression that is equivalent to "for ..."?
Is there any circumstance where it would be better to use "for"?

> Kind regards
>
> robert
>
Regards

--Fear Dubh


Robert Klemme

2/27/2005 8:41:00 PM

0


"Fear Dubh" <feardubh@spam.spam> schrieb im Newsbeitrag
news:cvt8g2$g47$1@reader01.news.esat.net...
> Hi,
>
> "Robert Klemme" <bob.news@gmx.net> wrote in message
> news:38ea7eF5o769pU1@individual.net...
>>
>> "Fear Dubh" <feardubh@spam.spam> schrieb im Newsbeitrag
>> news:cvslbj$9ns$1@reader01.news.esat.net...
>> >
>> > "ts" <decoux@moulon.inra.fr> wrote in message
>> > news:200502271128.j1RBSA019680@moulon.inra.fr...
>> >> >>>>> "F" == Fear Dubh <feardubh@spam.spam> writes:
>> >>
>> >> F> Is there a difference between:
>> >> F> for foo in bar do ...
>> >> F> and
>> >> F> bar.each do |foo| ...
>> >>
>> >> uln% ruby -e 'for a in [12] do end; p a'
>> >> 12
>> >> uln%
>> >>
>> >> uln% ruby -e '[12].each do |a| end; p a'
>> >> -e:1: undefined local variable or method `a' for main:Object
> (NameError)
>> >> uln%
>> >>
>> >>
>> > Thanks!
>> >
>> > Does each have a fatter frame then?
>> > Is "for ... in ... do ..." more efficient?
>>
>> I would believe not, but you can test this yourself (hint "ruby -r
> profile"
>> and module Benchmark).
>>
>> Indenpendently of that I strongly recommend the usage of "each" as it is
>> used 99% of the cases and it's simply standard. Although it's not an
>> enforced convention, people will expect that and it makes everybody's
>> life
>> easier to stick to some conventions.
>>
> OK, Thanks,
>
> Now I feel like a bold child, scolded for stuffing my face
> with this delicious syntax sugar! :-)

LOL

> I have a further question:
> As ts showed "for ... in ... do" is not exactly the same as "... each do
> ...",
> so is there an "un-sugared" expression that is equivalent to "for ..."?

Probably this:

x = nil
enum.each {|x| ...}
# x carries the last value here

> Is there any circumstance where it would be better to use "for"?

Frankly speaking, I can't remember having felt the need for "for". But you
can argue that it looks better for numeric ranges:

$ ruby -e 'for i in 1..5 do puts i end; puts i'
1
2
3
4
5
5

Robert@Babelfish2 ~
$ ruby -e 'i=nil; (1..5).each{|i| puts i}; puts i'
1
2
3
4
5
5

To be honest, I've completely forgotton "for .. in" and I use #each even for
numeric ranges - in spite of the uglyness. :-) Btw, there's also #upto,
#downto and #step:

$ ruby -e '1.step(10,2){|i| puts i}'
1
3
5
7
9

Robert@Babelfish2 ~
$ ruby -e '1.upto(5){|i| puts i}'
1
2
3
4
5

Robert@Babelfish2 ~
$ ruby -e '5.downto(1){|i| puts i}'
5
4
3
2
1


Kind regards

robert

Joel VanderWerf

2/27/2005 10:12:00 PM

0

Christian Neukirchen wrote:
> I strongly prefer #each over for, because it is more consistent and
> doesn't hide the fact that there really is a method call to #each.

I agree. Because of the consistency, it's very easy to convert an each
block to a map (or select or whatever) block, but harder to convert the
for construct.



Neil Stevens

2/27/2005 10:29:00 PM

0

On Sun, 27 Feb 2005 11:23:27 +0000, Fear Dubh wrote:
> Hello,
>
> Is there a difference between:
> for foo in bar do ...
> and
> bar.each do |foo| ...
>
> or is it just a question of style?
> I prefer the look of "for ... do ...",
>
> but I mostly see each in the samples posted by the Ruby
> wizards on the NG.

I use 'each' because I never learned the 'for', but I think I'll probably
start using for now that I've been reminded of it.

The less punctuation I have to use, the better. It's easier to type,
easier to read, and more descriptive of what it's doing.

I just hope I remember to use it in the future!

--
Neil Stevens - neil@hakubi.us

'A republic, if you can keep it.' -- Benjamin Franklin

Csaba Henk

2/28/2005 7:40:00 AM

0

On 2005-02-27, Robert Klemme <bob.news@gmx.net> wrote:
>
> "Fear Dubh" <feardubh@spam.spam> schrieb im Newsbeitrag
> news:cvslbj$9ns$1@reader01.news.esat.net...
>> Does each have a fatter frame then?
>> Is "for ... in ... do ..." more efficient?
>
> I would believe not, but you can test this yourself (hint "ruby -r profile"
> and module Benchmark).

$ ruby -v
ruby 1.8.2 (2004-12-25) [i386-linux]
ruby -rbenchmark -e 'n=10**6; [
proc{ (0...n).each{|x| x} },
proc{ for x in 0...n; x ;end },
proc{ n.times{|x| x} }
].each {|pr| puts Benchmark.measure(&pr) }'
0.370000 0.000000 0.370000 ( 0.376541)
0.320000 0.000000 0.320000 ( 0.339880)
0.370000 0.000000 0.370000 ( 0.391311)

So for is slightly faster. But see, do you wonder what happens if you
execute the above three benchmarks in reverse order? Just replace the
"each" (after the three-element array) with "reverse_each"... ;)

Csaba

Navindra Umanee

3/1/2005 5:39:00 AM

0

Csaba Henk <csaba@phony_for_avoiding_spam.org> wrote:
> ruby -rbenchmark -e 'n=10**6; [
> proc{ (0...n).each{|x| x} },
> proc{ for x in 0...n; x ;end },
> proc{ n.times{|x| x} }
> ].each {|pr| puts Benchmark.measure(&pr) }'

I have to say, I often wonder why people post Ruby code in this
format. It seems to me -e coupled with multi-line code is terribly
inconvenient... unless I'm missing something.

Cheers,
Navin.