the utility of until

● ARCHIVED · READ-ONLY
Started by nio kasgami 19 posts View original ↗
  1. Hello there

    I just noticed something in the manual who no one use  and it is the

    untilsince I never saw this in other script I thinked it was a non usefull key  but I was curious so I play a little with it and  it is pretty usefull for Condition method in script

    for be more understable I will explain with a damage control and invincible stat's

    def stats (hp,turn) @hp = hp @turn = turn do damage.nil until @turn < 10end(don't check my code I just put it for exemple)

    but what I wanted to say it a short way to create temporary condition

    Well it was I figure out about this
  2. I always thought that until was just a quick way to iterate a loop...

    for example...

    def do_crap  Graphics.brightness += 1  Graphics.updateend Graphics.brightness = 0do_crap until Graphics.brightness >= 255Never really used it though, so never had to investigate the inner workings.
  3. Dekita said:
    I always thought that until was just a quick way to iterate a loop...

    for example...

    def do_crap  Graphics.brightness += 1  Graphics.updateend Graphics.brightness = 0do_crap until Graphics.brightness >= 255Never really used it though, so never had to investigate the inner workings.
    well yes you can use in a loop method but it can be use for special condition like it loop the state's until the other thing's was inactive
  4. so I could do this ?

    array.each do |i| until i.not_to_be_read_for_whatever_reason  i.whatever_codeend
    Interesting...
  5. Yup but the only thing until is not usefull is for sprite it bug's a little you have to put a sliding movement or it will bug's
  6. Learn something new every day :p

    I would still preffer to just use a break in my array iteration but thats just personal preference i guess :)
  7. Dekita said:
    Learn something new every day :p

    I would still preffer to just use a break in my array iteration but thats just personal preference i guess :)
    yup break to is usefull x3 ! and yes is personnal preference owo!
  8. Dekita said:
    so I could do this ?

    array.each do |i| until i.not_to_be_read_for_whatever_reason  i.whatever_codeendInteresting...
    "until" is a loop, that code you posted won't work. Think of "until" as "while something is evaluated to be false".

    i = 0until i > 10 i += 1 p 1endi = 0while i < 10 i += 1 p 1end
    Dekita said:
    Learn something new every day :p

    I would still preffer to just use a break in my array iteration but thats just personal preference i guess :)
    You can use the break command in until loops too:

    Code:
    i = 0until i > 10  i += 1  p 1  break if i == 5endi = 0while i < 10  i += 1  p 1  break if i == 5end
  9. i think i still prefer the 'normal' loop method. something like this...

    Code:
    loop do  break if whatever  next if something else  # // do crapend
  10. You can switch between while and until to swallow negations:

     - while !condition is the same as until condition

     - until !condition is the same as while condition

    Moreover sometimes using either the one or the other more naturally expresses what you want to achieve. The machine doesn't care, but it can help scripters reading your code.

    *hugs*
  11. Zeriab said:
    You can switch between while and until to swallow negations:

     - while !condition is the same as until condition

     - until !condition is the same as while condition

    Moreover sometimes using either the one or the other more naturally expresses what you want to achieve. The machine doesn't care, but it can help scripters reading your code.

    *hugs*
    yup I usealy use it for a lot of my condition because is the only loop method I really understand and yes  the "!" can be user with the while and until

    and a lot of keyword can be use in a lot of way the only thing is limit you of how you use the keyword is your imagination ;w;!

    *paper hug!*
  12. In computer science terms, 'until' is a bottom check loop; 'while' is a top check loop.

    Top check loops are required to solve many algorithms (In fact they are one of only four control structures actually needed to solve anything). Bottom check loops are not. Anything you can do with a bottom check loop can be done with a top check loop. That's why the latter are far more common.

    It doesn't mean bottom check loops don't have their place - they can be very useful at times.

    'For' loops are redundant as well, being a specialized case of a 'while' loop, but they are a frequently used tool.

    Zeriab said:
    You can switch between while and until to swallow negations:

     - while !condition is the same as until condition

     - until !condition is the same as while condition

    Moreover sometimes using either the one or the other more naturally expresses what you want to achieve. The machine doesn't care, but it can help scripters reading your code.

    *hugs*
    Not quite. An 'until' loop is guaranteed to run once:

    do <stuff> until (condition)

    Stuff will get done once, even if condition=true when entering the loop.

    While (!condition)

    <do stuff>

    end

    If condition is true, stuff never gets done.
  13. Mouser said:
    Not quite. An 'until' loop is guaranteed to run once:

    do <stuff> until (condition)

    Stuff will get done once, even if condition=true when entering the loop.

    While (!condition)

    <do stuff>

    end

    If condition is true, stuff never gets done.
    No, it won't run once:

    Code:
    p 'run' until true
  14. Breaking Owl said:
    No, it won't run once:

    p 'run' until true
    huh why did you put it simply a true it normal is not work you don't define what it true so it not work

    you should do this :

    p 'run' until somethingbecause the système while considerate the statement will be alway's true you have to imput something to the the true
  15. nio kasgami said:
    huh why did you put it simply a true it normal is not work you don't define what it true so it not work

    you should do this :

    p 'run' until somethingbecause the système while considerate the statement will be alway's true you have to imput something to the the true
    Ruby doesn't care.

    Try this:

    something = truep 'run' until somethingp 'run' until trueI was answering to Mouser, he said that until was guaranteed to run once even if the condition was true when entering the loop.
  16. I stand corrected.

    In Ruby until and while are both top check loops, which makes until entirely unnecessary.

    That makes a very strong case for NOT using until in your code (IMHO, of course). It doesn't work as expected (only the way Matz expected) and is the sort of language idiosyncrasy that leads to confusing code and bugs down the line, especially if multiple people are working on the code, or will be maintaining it.
  17. You'd need to wrap the code in a begin ... end in order to make it function like a more traditional do ... while loop. This works:

    Code:
    begin  puts 'Defy logic.'end until true
  18. Solistra said:
    You'd need to wrap the code in a begin ... end in order to make it function like a more traditional do ... while loop. This works:

    begin puts 'Defy logic.'end until true
    Good to know. Thanks :)

    Bottom check loops are useful at times - glad to know there's an clear and easy way to do it in Ruby.
  19. Mouser said:
    That makes a very strong case for NOT using until in your code (IMHO, of course). It doesn't work as expected (only the way Matz expected) and is the sort of language idiosyncrasy that leads to confusing code and bugs down the line, especially if multiple people are working on the code, or will be maintaining it.
    I disagree. The intent is very clear (as Zeriab points out) and no one with a reasonable amount of experience in Ruby will be confused by it. It exists *solely* to make the programmer's intent more clear.


    Also, as to your previous point, "top check" loops are never required for any algorithm. Anything you can do with a while loop I can do with a do-while loop. You use whichever makes the most sense, but certainly I can add a `if (condition) break;` to the top of a do-while. For that matter, we don't "need" formal loop structures at all; goto works just fine.