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.*
On Thu, Aug 5, 2010 at 4:07 PM, David Kahn <[email protected]>wrote: > +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. > > > On Wed, Aug 4, 2010 at 9:26 AM, Pale Horse <[email protected]> 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... >> >> 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? >> -- >> Posted via http://www.ruby-forum.com/. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Talk" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]<rubyonrails-talk%[email protected]> >> . >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<rubyonrails-talk%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

