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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Pale Horse 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser wrote:> Pale Horse 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 > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.orgI''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. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Pale Horse 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 >> marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 >>> marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org >> >> 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 > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Aug 5, 1:26 am, Pale Horse <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 viahttp://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 5 August 2010 22:19, Kieran P <kieran776-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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/12I 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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
+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 <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org>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 <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> >> . >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Marquez <angel.marq...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org>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 <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 viahttp://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org. > >> To unsubscribe from this group, send email to > >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > >> . > >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.- Hide quoted text - > > - Show quoted text --- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.