next if and next unless vs if and unless in Ruby

What are the benefits of next if and next unless vs if and unless in Ruby?

Here are two examples I am referring to.

 next if file == '.' || file == '..'				
      next unless file.end_with?('.rb') || file.end_with?('.erb')

Open in new window

Who is Participating?
Paul JacksonConnect With a Mentor Software EngineerCommented:
Basically the use of next is used when looping, and moves to the next execution of the loop unless the condition following the next statement is met.

An if or unless statement is used to determine whether the block of code contained within the if/unless block is executed when the condition is met.
Mark_CoAuthor Commented:
so this line next if file == '.' || file == '..' basically says "move on and ignore this current iteration if you run into a . or a .. " ?
Paul JacksonSoftware EngineerCommented:
Yes that's correct.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.