Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #7305

Re: Nicest equivalent to counted "for" loop?

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.ruby
Subject Re: Nicest equivalent to counted "for" loop?
Date Fri, 2 Dec 2016 20:56:23 +0100
Lines 30
Message-ID <eae1v9F107iU1@mid.individual.net> (permalink)
References <36c7832c-aad6-4003-ae76-c392fbe591b7@googlegroups.com> <0df9807f-d5f3-4a27-bd98-d53eea83832f@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net QgbkXhmXXxaXaxqtj+v6MgKXgXbWRJDAdh+4igGklcE8fXBKU=
Cancel-Lock sha1:nzfTZ3Yc7blkXIXK1gzy2ZnsGzQ=
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0
In-Reply-To <0df9807f-d5f3-4a27-bd98-d53eea83832f@googlegroups.com>
Xref csiph.com comp.lang.ruby:7305

Show key headers only | View raw


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/

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Nicest equivalent to counted "for" loop? brec <sbrecher@gmail.com> - 2016-12-01 13:58 -0800
  Re: Nicest equivalent to counted "for" loop? sbrecher@gmail.com - 2016-12-01 14:01 -0800
    Re: Nicest equivalent to counted "for" loop? Robert Klemme <shortcutter@googlemail.com> - 2016-12-02 20:56 +0100
  Re: Nicest equivalent to counted "for" loop? "Robert L." <No_spamming@noWhere_7073.org> - 2017-01-15 14:03 +0000

csiph-web