From: Jason Roelofs Date: 2007-05-10T21:48:20+09:00 Subject: Re: loop questions ------=_Part_125235_24880690.1178801289709 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline i.times do |x| puts x end You have to specify what the internal variable is. As for the pascal code: 5.downto(1) do |x| puts x end Here are a few links to help out with these types of questions: Ruby Core API: http://ruby-doc.org/core/ Ruby Stdlib API: http://ruby-doc.org/stdlib Pragmatic Programmers Guide: http://www.rubycentral.com/book/ _why's poignant guide to Ruby (this one is odd): http://poignantguide.net/ruby/ Jason On 5/10/07, Lloyd Linklater wrote: > > I am new to ruby and loving it so far and have beginner's questions > about loops. > > I wanted something like this: > > for (int i = 1; i <= 5; i++) > printf("%d\n", [i]); > > expecting: > > 1 > 2 > 3 > 4 > 5 > > > I wrote this: > > i = 5 > > i.times do > puts i.to_s > end > > And got: > > 5 > 5 > 5 > 5 > 5 > > I am guessing that there is an internal variable that is incremented. > doh! Is there a way to get the changing variable in there without > having a separate variable in there? > > Anyway, I then tried this: > > for i in 1..5 > puts i.to_s > end > > Which worked as expected. But I had better use for a descending loop. > In pascal it would be > > for i := 5 downto 1 do > writeln('%d', [i]); > > but I could not figure out how to do that in a for loop. I looked > through several books and could not find the answer. Any tips? > > -- > Posted via http://www.ruby-forum.com/. > > ------=_Part_125235_24880690.1178801289709--