How `next` works in ruby with `unless`?

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 ?

Learn what semicolons do in Ruby.

What Joel said.

Also, why are you calling ‘next nil’ ?

Matthew K. wrote in post #1100097:

What Joel said.

Also, why are you calling ‘next nil’ ?

My interest is only with that part :

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 rest one I pasted here by mistake. Sorry for that. Please ignore the
second part.

On 5 March 2013 08:26, Cliff R. [email protected] wrote:

People are so incredibly brash on this forum sometimes. Makes for a cold
community. :slight_smile:

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.

People are so incredibly brash on this forum sometimes. Makes for a cold
community. :slight_smile:

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

2hi
3hi
4hi
5"good"

??

http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html -
look at the “if and unless modifiers” section.

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.

John W Higgins wrote in post #1100104:

What do you think the only possible order of execution would be? It’s
not

I did that after reading the description - Thenextstatement 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 condition. Just to see that behavior and for which I wrote
the
line : "next puts “hi” unless n == 5 " .

mixing too many concepts for no gain at all.

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 <

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. :slight_smile:

Cheers,
Garth

Am 04.03.2013 23:26, schrieb Cliff R.:

People are so incredibly brash on this forum sometimes. Makes for a cold
community. :slight_smile:

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 :slight_smile:

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…

On 5 March 2013 14:25, Robert K. [email protected] wrote:

Is it really proven that there is actually a single person behind various
nicks?

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 :frowning:

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.

On Tue, Mar 5, 2013 at 7:13 AM, [email protected] wrote:

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 :slight_smile:

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

Kumar R. wrote in post #1100105:

John W Higgins wrote in post #1100104:

What do you think the only possible order of execution would be? It’s
not

I did that after reading the description - Thenextstatement 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 ?

Am 05.03.2013 19:18, schrieb Kumar R.:

Kumar R. wrote in post #1100105:

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 ?

Nobody writes code like this, so why care.

Kumar R. wrote in post #1100229:

Kumar R. wrote in post #1100105:

John W Higgins wrote in post #1100104:

What do you think the only possible order of execution would be? It’s
not

I did that after reading the description - Thenextstatement 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)

On Tue, Mar 5, 2013 at 7:23 PM, [email protected]invalid wrote:

executed. Why does next not skip this statement ?
Probably because arguments are always evaluated before the “method”
they are handed over to.

Nobody writes code like this, so why care.

I wasn’t even aware that next accepts an argument - funnily it won’t
accept two (syntax error).

Cheers

robert

Robert K. wrote in post #1100233:

On Tue, Mar 5, 2013 at 7:23 PM, [email protected] wrote:

executed. Why does next not skip this statement ?
Probably because arguments are always evaluated before the “method”
they are handed over to.

Nobody writes code like this, so why care.

I wasn’t even aware that next accepts an argument - funnily it won’t
accept two (syntax error).

Cheers

robert

hm it does:

[1,2,3,4].map {|d| next d,4,5;p “is not printed” }
#=> [[1, 4, 5], [2, 4, 5], [3, 4, 5], [4, 4, 5]]

On Tue, Mar 5, 2013 at 7:38 PM, Hans M. [email protected]
wrote:

Robert K. wrote in post #1100233:

On Tue, Mar 5, 2013 at 7:23 PM, [email protected] wrote:

executed. Why does next not skip this statement ?
Probably because arguments are always evaluated before the “method”
they are handed over to.

Nobody writes code like this, so why care.

I wasn’t even aware that next accepts an argument - funnily it won’t
accept two (syntax error).

hm it does:

[1,2,3,4].map {|d| next d,4,5;p “is not printed” }
#=> [[1, 4, 5], [2, 4, 5], [3, 4, 5], [4, 4, 5]]

Oh, I tested only with brackets. Here’s the whole story

$ ruby -ce ‘f { next }’
Syntax OK
$ ruby -ce ‘f { next 1 }’
Syntax OK
$ ruby -ce ‘f { next 1, 2 }’
Syntax OK
$ ruby -ce ‘f { next 1, 2, 3 }’
Syntax OK
$ ruby -ce ‘f { next(1) }’
Syntax OK
$ ruby -ce ‘f { next(1, 2) }’
-e:1: syntax error, unexpected ‘,’, expecting ‘)’
f { next(1, 2) }
^

It’s getting weirder - especially since the value(s) passed to next
are not used for anything as far as I can see. And it wouldn’t make
sense, would it? I mean, next is different from break which can be
used to return a value from an iteration.

Cheers

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs