Does ruby have anything where if I''m going through a do-while loop, or a "3.times" loop or "for product in products" where there''s a variable that will tell me which iteration of the loop I''m on? Instead of havign to declare my own counter = 0 and then increment it each time? Seems like something Rails would have... I can''t seem to find anything on it. Thanks guys. --~--~---------~--~----~------------~-------~--~----~ 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 For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Aug-15 16:28 UTC
Re: Is there a automatic counter variable?
Hi -- On Wed, 15 Aug 2007, sw0rdfish wrote:> > Does ruby have anything where if I''m going through a do-while loop, or > a "3.times" loop or "for product in products" where there''s a variable > that will tell me which iteration of the loop I''m on? Instead of > havign to declare my own counter = 0 and then increment it each time? > > Seems like something Rails would have... I can''t seem to find > anything on it.3.times {|i| puts "Iteration #{i}" } Also: %w{a b c d e f}.each_with_index {|letter,i| puts "#{letter} is at index #{i}" } David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.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 For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
sw0rdfish wrote:> Does ruby have anything where if I''m going through a do-while loop, or > a "3.times" loop or "for product in products" where there''s a variable > that will tell me which iteration of the loop I''m on? Instead of > havign to declare my own counter = 0 and then increment it each time? > > Seems like something Rails would have... I can''t seem to find > anything on it. > > Thanks guys. > >something like this? (2..46).each {|i| puts "i is now: #{i}" } --~--~---------~--~----~------------~-------~--~----~ 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 For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Does ruby have anything where if I''m going through a do-while loop, or > a "3.times" loop or "for product in products" where there''s a variable > that will tell me which iteration of the loop I''m on? Instead of > havign to declare my own counter = 0 and then increment it each time?irb(main):001:0> 5.times {|i| puts i } 0 1 2 3 4 irb(main):002:0> [''one'', ''two'', ''three''].each_with_index {|i,e| puts i, e} one 0 two 1 three 2 Don''t know if "for product in products" has anything, but you could always do "products.each_with_index". -philip --~--~---------~--~----~------------~-------~--~----~ 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 For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hey guys... Looks good, the with_index is the option I was looking for... just looked so messy having to declare a counter variable in a view... thanks! On Aug 15, 12:33 pm, Mohit Sindhwani <mo_m...-RxrYI66vbj0AvxtiuMwx3w@public.gmane.org> wrote:> sw0rdfish wrote: > > Does ruby have anything where if I''m going through a do-while loop, or > > a "3.times" loop or "for product in products" where there''s a variable > > that will tell me which iteration of the loop I''m on? Instead of > > havign to declare my own counter = 0 and then increment it each time? > > > Seems like something Rails would have... I can''t seem to find > > anything on it. > > > Thanks guys. > > something like this? > > (2..46).each {|i| > puts "i is now: #{i}" > }--~--~---------~--~----~------------~-------~--~----~ 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 For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---