the utility of until

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
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
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,202
First Language
Binary
Primarily Uses
RMMZ
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.
 
Last edited by a moderator:

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
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
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,202
First Language
Binary
Primarily Uses
RMMZ
so I could do this ?

array.each do |i| until i.not_to_be_read_for_whatever_reason  i.whatever_codeend
Interesting...
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
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
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,202
First Language
Binary
Primarily Uses
RMMZ
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 :)
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
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!
 

♥SOURCE♥

Too sexy for your party.
Veteran
Joined
Mar 14, 2012
Messages
693
Reaction score
411
Primarily Uses
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
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
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,202
First Language
Binary
Primarily Uses
RMMZ
i think i still prefer the 'normal' loop method. something like this...

Code:
loop do  break if whatever  next if something else  # // do crapend
 

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,268
Reaction score
1,422
First Language
English
Primarily Uses
RMXP
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*
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
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!*
 

Mouser

Veteran
Veteran
Joined
Aug 19, 2012
Messages
1,245
Reaction score
264
First Language
English
Primarily Uses
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.

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.
 
Last edited by a moderator:

♥SOURCE♥

Too sexy for your party.
Veteran
Joined
Mar 14, 2012
Messages
693
Reaction score
411
Primarily Uses
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
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
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
 

♥SOURCE♥

Too sexy for your party.
Veteran
Joined
Mar 14, 2012
Messages
693
Reaction score
411
Primarily Uses
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.
 

Mouser

Veteran
Veteran
Joined
Aug 19, 2012
Messages
1,245
Reaction score
264
First Language
English
Primarily Uses
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.
 
Last edited by a moderator:

Solistra

Veteran
Veteran
Joined
Aug 15, 2012
Messages
593
Reaction score
247
Primarily Uses
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
 

Mouser

Veteran
Veteran
Joined
Aug 19, 2012
Messages
1,245
Reaction score
264
First Language
English
Primarily Uses
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.
 

BigEd781

undefined method 'stupid_title' found for nil:NilC
Veteran
Joined
Mar 1, 2012
Messages
940
Reaction score
304
First Language
Dothraki
Primarily Uses
N/A
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.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:
To whom ever person or persons who re-did the DS/DS+ asset packs for MV (as in, they are all 48x48, and not just x2 the pixel scale) .... THANK-YOU!!!!!!!!! XwwwwX

Forum statistics

Threads
105,849
Messages
1,016,977
Members
137,563
Latest member
cexojow
Top