f



Nicest equivalent to counted "for" loop?

Currently I've got...

$Count = 192 # some arbitrary constant, or command line argument, or input; > 0

$i = $Count
while (i -= 1) >= 0
   # some stuff
end

....In other languages I'd use a counted "for" loop. Is there something more elegant than the above?
0
brec
12/1/2016 9:58:15 PM
comp.lang.ruby 48886 articles. 0 followers. Post Follow

2 Replies
554 Views

Similar Articles

[PageSpeed] 33

On Thursday, December 1, 2016 at 1:58:24 PM UTC-8, brec wrote:
> Currently I've got...
> 
> $Count = 192 # some arbitrary constant, or command line argument, or input; > 0
> 
> $i = $Count
> while (i -= 1) >= 0
>    # some stuff
> end
> 
> ...In other languages I'd use a counted "for" loop. Is there something more elegant than the above?

Kindly ignore the missing "$" in the conditional expression.
0
sbrecher
12/1/2016 10:01:04 PM
On 01.12.2016 23:01, sbrecher@gmail.com wrote:
> On Thursday, December 1, 2016 at 1:58:24 PM UTC-8, brec wrote:
>> Currently I've got...
>>
>> $Count = 192 # some arbitrary constant, or command line argument, or input; > 0
>>
>> $i = $Count
>> while (i -= 1) >= 0
>>    # some stuff
>> end
>>
>> ...In other languages I'd use a counted "for" loop. Is there something more elegant than the above?
>
> Kindly ignore the missing "$" in the conditional expression.

$i.times do |x|
end

$i.downto 0 do |x|
end

Off by one errors included. ;-)

Cheers

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
0
Robert
12/2/2016 7:56:23 PM
Reply: