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?
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.
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.
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.
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.
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.
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.
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.
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.
...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).
+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.
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)
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)