• Nicest equivalent to counted "for" loop?

    From brec@21:1/5 to All on Thu Dec 1 13:58:15 2016
    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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From sbrecher@gmail.com@21:1/5 to brec on Thu Dec 1 14:01:04 2016
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Klemme@21:1/5 to sbrecher@gmail.com on Fri Dec 2 20:56:23 2016
    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/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert L.@21:1/5 to brec on Sun Jan 15 14:03:57 2017
    On 12/1/2016, 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?

    3.downto(0){|i| p i}
    3
    2
    1
    0

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)