The `next` statement is used to skip a part of the loop and continue
with the next iteration of the loop. It can be used in combination with
`for` and `while` statements.
I have seen people using next if there is complicated piece of code
after some condition is being evaluated i.e
next if @state!=:some_state
# some long complicated code
Now here I have played with the `next` in my `IRB` as below :
n = 1
loop do
n = n + 1
next unless n == 10
print "Good"
break
end
# Good=> nil
The above one understood. Nicely clear.
n = 1
#=> 1
loop do
print "#{n}"
n = n + 1
next puts "hi" unless n == 5
p "good"
break
end #1hi #2hi #3hi #4"good"
#=> nil
In the above code, couldn't understand about which order the lines `puts
"hi"` and `unless n == 5` executed. Which executed first?
The below one leads to the `infinite Loop`.
n = 1
#=> 1
loop do
print "#{n}"
n = n + 1
next puts "hi"; 2 + 3 unless n == 5
p "good"
break
end
whereas this one is good:
n = 1
#=> 1
loop do
print "#{n}"
n = n + 1
next puts "hi", 2 + 3 unless n == 5
p "good"
break
end
#1hi #5 #2hi #5 #3hi #5 #4"good"
#=> nil
Please help me here to understand - how does this one resolve that
`forever` loop ?
They all use backticks instead of quotes. They all write with the same
cadence. They all use similar or identical terminology. They are all
accounts created recently and in sequence, corresponding to complaints
about the previous ID's activity. Looks pretty likely to be the same
vampire to me.
they values you used for next are returned by yield ...
(so all block functions that does something different depending on the
block result does be affected like: .map, .select, .find, .group_by and
others )
irb(main):025:0> 10.times.map {|i| next if i == 2;i*10}
=> [0, 10, nil, 30, 40, 50, 60, 70, 80, 90]
"next" is for a block about the same as a "return" for a method: the
current call is terminated at this point with the provided arguments
given to the caller (the "yield"). And since "next" is a keyword it
does make sense to have it follow the same syntax rules for arguments
like "return" and "break".
Hans, thank you for the heads up!
Kind regards
robert
···
On Tue, Mar 5, 2013 at 8:07 PM, Hans Mackowiak <lists@ruby-forum.com> wrote:
they values you used for next are returned by yield ...
(so all block functions that does something different depending on the
block result does be affected like: .map, .select, .find, .group_by and
others )
What do you think the only possible order of execution would be? It's not
difficult to look at that and figure out exactly which section is executed
first
Also - if you are going to test something - at least print out the variable
in question AFTER you increment it - it makes your "debugging output" look
rather much less helpful to you.
Maybe if you got this it would make more sense to you
There is also the notion that there is ZERO reason for you to need to write
something more complex then necessary when learning something.
This works just as fine and you would understand it.
n = 1
loop do
n = n + 1
print "#{n}"
if n < 5
puts 'hi'
next
end
p 'good'
break
end
You are continually trying to add 20 different unknown concepts into your
thought stream - take things one at a time - learn them well - then start
mixing and matching concepts to see how it comes together.
John
BTW - even if the parser takes the next puts 'hi' unless n == 5 concept -
that's really just not code that is used by anyone that I've seen. Again,
mixing too many concepts for no gain at all.
Only to certain users. I know it reflects on us poorly, but we've been
over things like "the return value of puts" before with this (or an
identical?) user.
The line:
next puts "hi"
is already weird, without then conditionally executing it with `unless n ==
5`
The question should be: how did you get your code into this state in the
first place? Rather than trying to understand how the interpreter handles
it.
···
On 5 March 2013 08:26, Cliff Rosson <cliff.rosson@gmail.com> wrote:
People are so incredibly brash on this forum sometimes. Makes for a cold
community.
What do you think the only possible order of execution would be? It's
not
I did that after reading the description - `The `next` statement is used
to skip a part of the loop and continue with the next iteration of the
loop.`
This is too straight statement and I understood. I just tried the above
to see what was not mentioned in that description - which is - How
`next` reacts when it would get anything between it and `while` or `if`
or `unless` loop. Just to see that behavior and for which I wrote the
line : "next puts "hi" unless n == 5 " .
Unfortunately, for the last little bit, the list has been under siege by a single person posting with multiple pseudonyms, posting a barrage of ill-formed questions in strained, tortured English, disrespecting the time and effort of the people who take the time to respond by outright ignoring the answers, and asking each person responding personally for more and more information, whilst refusing to make any effort to apply the information gained; until the poor person, who was only trying to help in the first place, finally gives up out of frustration. Attempts to either reason with, prevent, or call out this person on their behaviour, are met with type of righteous indignation that can only be mustered by someone with a complete and utter disregard for anyone and anything but themselves and their own personal convenience- or someone simply enjoying the frustration that they are causing by acting in this way. Sometimes it can be hard to tell the difference.
People are dealing with this noise in their own way, and there is some disagreement about how to handle it. Some people are, understandably, a bit on edge at the moment. The list, sadly, will be worse for it.
Eventually things will settle down again, and the community will again appear to be as it has (always?) been- a collection of genuinely considerate and helpful people messing about with the intricacies of an interesting and useful language.
Cheers,
Garth
···
On 05/03/13 08:56, Cliff Rosson wrote:
People are so incredibly brash on this forum sometimes. Makes for a cold
community.
I always perceived the list members as *extremely* friendly,
patient, helpful, and willing to share their profound knowledge.
A big "Thank you" to all for that
Only at the moment I think the patience of many list members is
stretched to its limits, because some user keeps posting (under
various aliases) pointless, randomly cobbled together code
that he cannot understand because he keeps ignoring our advice
and refuses to gain a basic understanding of the language.
It's sad that this reflects badly on the list itself...
···
Am 04.03.2013 23:26, schrieb Cliff Rosson:
People are so incredibly brash on this forum sometimes. Makes for a cold
community.
Yes. I, as a lurker, I have more or less picked up on the fact that
something was going on. Seeing some of these threads out of context is
sometimes hard to place. Hopefully things will improve in the future!
···
On Mon, Mar 4, 2013 at 6:56 PM, Garthy D < garthy_lmkltybr@entropicsoftware.com> wrote:
Hi Cliff,
Unfortunately, for the last little bit, the list has been under siege by a
single person posting with multiple pseudonyms, posting a barrage of
ill-formed questions in strained, tortured English, disrespecting the time
and effort of the people who take the time to respond by outright ignoring
the answers, and asking each person responding personally for more and more
information, whilst refusing to make any effort to apply the information
gained; until the poor person, who was only trying to help in the first
place, finally gives up out of frustration. Attempts to either reason with,
prevent, or call out this person on their behaviour, are met with type of
righteous indignation that can only be mustered by someone with a complete
and utter disregard for anyone and anything but themselves and their own
personal convenience- or someone simply enjoying the frustration that they
are causing by acting in this way. Sometimes it can be hard to tell the
difference.
People are dealing with this noise in their own way, and there is some
disagreement about how to handle it. Some people are, understandably, a bit
on edge at the moment. The list, sadly, will be worse for it.
Eventually things will settle down again, and the community will again
appear to be as it has (always?) been- a collection of genuinely
considerate and helpful people messing about with the intricacies of an
interesting and useful language.
Cheers,
Garth
On 05/03/13 08:56, Cliff Rosson wrote:
People are so incredibly brash on this forum sometimes. Makes for a cold
community.
I always perceived the list members as *extremely* friendly,
patient, helpful, and willing to share their profound knowledge.
A big "Thank you" to all for that
You're welcome!
Only at the moment I think the patience of many list members is
stretched to its limits, because some user keeps posting (under
various aliases) pointless, randomly cobbled together code
that he cannot understand because he keeps ignoring our advice
and refuses to gain a basic understanding of the language.
Is it really proven that there is actually a single person behind various nicks?
It's sad that this reflects badly on the list itself...
We're all human. I believe some people did not yet fully realize that
you cannot *force* someone to learn or change in the ways one thinks
is best for them. If one realizes that one's advice is not heard then
it's usually best to just move on.
Cheers
robert
···
On Tue, Mar 5, 2013 at 7:13 AM, <sto.mar@web.de> wrote:
What do you think the only possible order of execution would be? It's
not
I did that after reading the description - `The `next` statement is used
to skip a part of the loop and continue with the next iteration of the
loop.`
line : "next puts "hi" unless n == 5 " .
next generally used to skip the part of execution and send the control
to the beginning. But how the in between part puts "hi" has been
executed. Why does next not skip this statement ?
I'm not entirely sure about this. Love U Ruby had
some idiosyncratic phrases that none of the others seem to have used, "bad
mentalities" was one. He was fond of the word "mentalities". I do not think
any of the other have used it. But this does not mean that the rest are not
themselves sockpuppets of another party.
Having said that things seem to happen in clusters so I am willing to
believe that we have suddenly had a bunch of oxygen thieves on our hands.
This is the nature of the eternal september
···
On 5 March 2013 14:25, Robert Klemme <shortcutter@googlemail.com> wrote:
Is it really proven that there is actually a single person behind various
nicks?
next generally used to skip the part of execution and send the control
to the beginning. But how the in between part puts "hi" has been
executed. Why does next not skip this statement ?
What do you think the only possible order of execution would be? It's
not
I did that after reading the description - `The `next` statement is used
to skip a part of the loop and continue with the next iteration of the
loop.`
line : "next puts "hi" unless n == 5 " .
next generally used to skip the part of execution and send the control
to the beginning. But how the in between part puts "hi" has been
executed. Why does next not skip this statement ?
its because puts "hi" is evaluated first and THEN the result of it is
given to the next keyword (in the case of puts it is nil)