FOR or EACH?

When looping through arrays. What’d you argue to be good (or best)
practice?

<% for something in @lots_of_things %>
<%= something.name %>
<% end %>

<% @lots_of_things.each do |something| %>
<%= something.name %>
<% end %>

I’ve read mixed opinions on this small topic. Some say that FOR loops
are easier, though I cannot see how. Others say that EACH is a more
‘railsy’ method…

Personally, I tend to use EACH as I was taught this to be correct and
follow this rule to retain consistency in MY code. What is your opinion
on this?

Pale H. wrote:

When looping through arrays. What’d you argue to be good (or best)
practice?

<% for something in @lots_of_things %>
<%= something.name %>
<% end %>

<% @lots_of_things.each do |something| %>
<%= something.name %>
<% end %>

I’ve read mixed opinions on this small topic. Some say that FOR loops
are easier, though I cannot see how. Others say that EACH is a more
‘railsy’ method…

Not Railsy so much as Rubyish.

Personally, I tend to use EACH as I was taught this to be correct and
follow this rule to retain consistency in MY code. What is your opinion
on this?

I do likewise. To tell you the truth, I don’t even remember that the
for construct exists until I see it in other people’s code.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Pale H. wrote:

When looping through arrays. What’d you argue to be good (or best)
practice?

<% for something in @lots_of_things %>
<%= something.name %>
<% end %>

<% @lots_of_things.each do |something| %>
<%= something.name %>
<% end %>

I’ve read mixed opinions on this small topic. Some say that FOR loops
are easier, though I cannot see how. Others say that EACH is a more
‘railsy’ method…

Not Railsy so much as Rubyish.

Exactly.

Personally, I tend to use EACH as I was taught this to be correct and
follow this rule to retain consistency in MY code. What is your opinion
on this?

I do likewise. To tell you the truth, I don’t even remember that the
for construct exists until I see it in other people’s code.

Indeed, and old code at that in my case. I remember it from when briefly
I looked into C.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I’m interested to get people’s input on this. Some Rails programmers
still persist on using FOR. Understandable if they have extensive
programming background including Ruby.

Pale H. wrote:
[…]

I do likewise. To tell you the truth, I don’t even remember that the
for construct exists until I see it in other people’s code.

Indeed, and old code at that in my case. I remember it from when briefly
I looked into C.

The for loop in C is like the for loop in BASIC, not the for…in loop
in Ruby. Ruby’s for…in is like the for…in (or foreach) in Perl,
PHP, or recent versions of Java. Despite the use of the same keyword,
they’re two very different constructs.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I’m interested to get people’s input on this. Some Rails programmers
still persist on using FOR. Understandable if they have extensive
programming background including Ruby.

…which I do. But for…in just doesn’t feel like a good fit for Ruby
to me – it feels more like syntactic sugar for Perl and PHP
programmers.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Hey,

I personally use .each, but if you’re looking to squeeze every bit of
performance from your app, use for, as it’s about 7% faster
than .each.

http://rubybenchmark.com/reports/12

Regards
Kieran

On 5 August 2010 22:19, Kieran P [email protected] wrote:

Hey,

I personally use .each, but if you’re looking to squeeze every bit of
performance from your app, use for, as it’s about 7% faster
than .each.

http://rubybenchmark.com/reports/12

I can’t comment on the accuracy or otherwise of the benchmark itself,
but note that even if it is correct then the 7% faster will only be
for a virtually empty loop. If you put anything worthwhile in the
loop then I expect that any difference between iteration methods will
be swamped by the processing of the loop contents.

Colin

Hi –

On Wed, 4 Aug 2010, Marnen Laibow-Koser wrote:

…which I do. But for…in just doesn’t feel like a good fit for Ruby
to me – it feels more like syntactic sugar for Perl and PHP
programmers.

Evidence for that can be found in the fact that for is implemented in
terms of each:

obj = Object.new
def obj.each
puts “Here I am in each, about to yield 100!”
yield 100
end

for a in obj
puts a
end

Output:
Here I am in each, about to yield 100!
100

Another demo:

$ ruby -e ‘for a in 3; end’
-e:1:in <main>': undefined method each’ for 3:Fixnum
(NoMethodError)

There’s a slight difference in how they work, in the sense that each
takes a code block (at least, as usually implemented), while for is in
flat scope, similar to an if-statement:

for a in [1]
b = 100
end

p b # 100

I don’t know of any cases where having the flat scope would be so
important as to lead me to use for if I didn’t have some other reason to
(which I don’t think I ever have).

David


David A. Black, Senior Developer, Cyrus Innovation Inc.

The Ruby training with Black/Brown/McAnally
Compleat Philadelphia, PA, October 1-2, 2010
Rubyist http://www.compleatrubyist.com

Marnen Laibow-Koser wrote:

The for loop in C is like the for loop in BASIC, not the for…in loop
in Ruby. Ruby’s for…in is like the for…in (or foreach) in Perl,
PHP, or recent versions of Java. Despite the use of the same keyword,
they’re two very different constructs.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I’m interested to get people’s input on this. Some Rails programmers
still persist on using FOR. Understandable if they have extensive
programming background including Ruby.

…which I do. But for…in just doesn’t feel like a good fit for Ruby
to me – it feels more like syntactic sugar for Perl and PHP
programmers.

That’s likely to be the reason for the choice to use FOR, then.
Interesting.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

The design patterns book on Ruby I’m checking out says this:

Ruby also has a for loop, which you can use, among other things, to
sequence

through arrays:

  • array = [‘first’, ‘second’, ‘third’]*
  • array.each do |x|*
  •  puts(x)*
    
  • end*

Surprisingly, for loops are rare in real Ruby programs. A Ruby
programmer
is

much more likely to write this equivalent code instead:

  • array.each do |x|*
  •  puts(x)*
    
  • end*

We will have much more to say about this odd-looking loop thing in
Chapter
7.

For now, just think of the each syntax as another way to write a for
loop.

Can’t remember which book I saw it in (possibly Metaprogramming Ruby)
but at the beginning there is a statement along the lines of:

Think of an array. Now think of accessing that array. If your first
thought is a for loop then you need to read more about Ruby, if your
first thought is .each then you are ready to begin the concepts in
this book… (something like that)

FWIW

Paul

On Aug 6, 12:22 am, Angel Robert M. [email protected]

+1 for each (second). To me, the for reminds me of other languages where
you
have to tell it what type of item the collection you are iterating
contains
(I am thinking of C#), but in Ruby is not really an issue. I would be
interested in other opinions but to me not using ‘for’ just seems
‘right’
and an easier read, maybe b/c this is what I seem to see most often.

paul h wrote:

Can’t remember which book I saw it in (possibly Metaprogramming Ruby)
but at the beginning there is a statement along the lines of:

Think of an array. Now think of accessing that array. If your first
thought is a for loop then you need to read more about Ruby, if your
first thought is .each then you are ready to begin the concepts in
this book… (something like that)

I’d be in agreement with that.

FWIW

Paul