for vs. each

F

Fear Dubh

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
 
T

ts

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
 
F

Fear Dubh

ts said:
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
 
R

Robert Klemme

Fear Dubh said:
ts said:
"F" == Fear Dubh <[email protected]> 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
 
F

Fear Dubh

Hi,

Robert Klemme said:
Fear Dubh said:
ts said:
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
 
R

Robert Klemme

Fear Dubh said:
Hi,

Robert Klemme said:
Fear Dubh said:
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
 
J

Joel VanderWerf

Christian said:
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.
 
N

Neil Stevens

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

Csaba Henk

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
 
N

Navindra Umanee

Csaba Henk said:
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.
 
G

Gavri Fernandez

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

In eRuby templates when used with html.
The each() construct just doesn't gel with html.

<%for item in @items%>
<tag>.........</tag>
<%end>

looks way better than

<%@items.each do%>
<tag>............</tag>
<%end%>

That though is just my opinion. Whatever makes you happy, folks :)
 
J

Jamis Buck

In eRuby templates when used with html.
The each() construct just doesn't gel with html.

<%for item in @items%>
<tag>.........</tag>
<%end>

looks way better than

<%@items.each do%>
<tag>............</tag>
<%end%>

That though is just my opinion. Whatever makes you happy, folks :)

It's definitely a matter of personal esthetics. To me, the #each
invocation looks cleaner. :)

- Jamis
 
C

Csaba Henk

Csaba Henk said:
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.

If you want to try the code yourself, you can just copy & paste & run
as-is.

With any posted code you can do that you type "ruby -e '", paste the
code, add one more single quote, and hit enter... just before doing that
you should check whether are there single quotes in the code.

With this form you don't have to think about such things. And I don't
think that the fact that this code is prefixed by "ruby -rbenchmark -e '"
makes it unreadable. YMMV, tho.

I don't see how that -e thingy would affect the convenience of the code,
be it a real one liner or a pseudo one :)

Csaba
 
D

Dave Burt

Csaba Henk said:
Csaba Henk said:
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.

If you want to try the code yourself, you can just copy & paste & run
as-is.
...
I don't see how that -e thingy would affect the convenience of the code,
be it a real one liner or a pseudo one :)

It affects the convenience on Windows, where single-quotes don't work.

What works best here in my experience is a straight ruby script; you can
feed it directly to the ruby interpreter on STDIN on any platform I know.

Cheers,
Dave
 
S

Steven Shaw

I tried this on my machine with a slightly modified ruby script:

===
# simulate 'ruby --version'
puts "ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"

require 'benchmark'

n = 10**6
a = [
{:name => "each ",
:proc => proc{ (0...n).each{|x| x}}},
{:name => "for ",
:proc => proc{ for x in 0...n; x ;end }},
{:name => "times ",
:proc => proc{ n.times{|x| x}}}
]
a.each {
|o| puts "#{o[:name]} #{Benchmark.measure(&o[:proc])}"
}
puts
a.reverse_each {
|o| puts "#{o[:name]} #{Benchmark.measure(&o[:proc])}"
}

puts <<END

Csaba's benchmark figures were:
ruby 1.8.2 (2004-12-25) [i686-linux]
each 0.370000 0.000000 0.370000 ( 0.376541)
for 0.320000 0.000000 0.320000 ( 0.339880)
times 0.370000 0.000000 0.370000 ( 0.391311)
END
===


Running this:

steve@ruby2 ~/Documents/src/Ruby $ uname -a
Linux ruby2 2.6.10-gentoo-r6 #3 Thu Feb 24 11:29:50 EST 2005 i686
Pentium III (Coppermine) GenuineIntel GNU/Linux
steve@ruby2 ~/Documents/src/Ruby $ ruby benchmark.rb
ruby 1.8.2 (2004-12-25) [i686-linux]
each 0.780000 0.000000 0.780000 ( 0.850872)
for 0.560000 0.000000 0.560000 ( 0.586458)
times 0.750000 0.000000 0.750000 ( 0.779069)

times 0.750000 0.000000 0.750000 ( 0.831255)
for 0.560000 0.000000 0.560000 ( 0.589044)
each 0.770000 0.000000 0.770000 ( 0.801004)

Csaba's benchmark figures were:
ruby 1.8.2 (2004-12-25) [i686-linux]
each 0.370000 0.000000 0.370000 ( 0.376541)
for 0.320000 0.000000 0.320000 ( 0.339880)
times 0.370000 0.000000 0.370000 ( 0.391311)


For me, "for" is quite alot quicker than either mechanism that uses
blocks. At the moment I'm not really concerned with performance. It's
interesting though. I seem to be running the same version of ruby as
Csaba but my hardware is letting me down - particularly with blocks...

Steve.
 

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,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top