Table of contents
1.
Introduction
2.
Iterators in Ruby
3.
Defining Iterators
3.1.
Example
3.2.
Output
4.
Problem Statement 
5.
Break Statement
5.1.
Example 
5.2.
Output
5.3.
Explanation 
6.
Next Statement
6.1.
Example
6.2.
Output
6.3.
Explanation 
7.
Frequently Asked Questions
7.1.
What do you mean by Ruby?
7.2.
What is an iterator in Ruby?
7.3.
What is the defining an iterator in ruby?
7.4.
Which statement is used for stopping an iteration in Ruby?
7.5.
What is the next statement used for?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Stopping an Iteration in Ruby

Author Ayushi Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Ruby is a high-level, interpreted, and object-oriented programming language that is general-purpose, dynamic, and reflective. Except for blocks, which have replacements in the form of procs and lambda, everything in Ruby is an object. The goal of Ruby’s development was to create a user interface between human programmers and the computational machinery that underpins them.

Introduction to ruby

Ruby's syntax is similar to many other programming languages, including C and Java, making learning easy for Java and C programmers. It runs on various OS(Operating Systems), including Windows, Mac OS X, and Linux.

Let's talk about the methods in ruby and how to define them in Ruby.

Iterators in Ruby

Iteration refers to doing one thing many times. Iterators are object-oriented concepts and are used for performing these iterations in Ruby. Iterators are supported by collections(Arrays, Hash, etc.) in Ruby and help return all the collection elements. There are many iterators in Ruby.

ITERATOR

Use of iterator

EACH

This iterator returns all the elements of a hash or an array.

STEP

When the user wants to skip some specific range, then this iterator is used.

TIMES

This iterator is used to implant a loop for a specified number of times starting from zero.

UPTO

This iterator follows the top to bottom approach.

COLLECT

This iterator returns all the elements of the whole collection, whether it is a hash or an array.

DOWNTO

This iterator follows the bottom-to-top approach, i.e., it includes both an iteration's starting and ending variable. 

EACH_LINE

To iterate over a new line in a string, this each_line iterator is used.  

 

Defining Iterators

The syntax for defining an iterator is:

collection.iterator do |variable|
	# Statement
end
You can also try this code with Online Ruby Compiler
Run Code

Example

array = [1,2,3,4,5]
print "Iterator result - \n"
array.each do |var|
   print var
   print " "
end
You can also try this code with Online Ruby Compiler
Run Code

Output

Output of defining iterators

Problem Statement 

Problem statement

We have already learned about the different types of iterators and how to define them. So now come to the main topic, i.e., Stopping an iteration in Ruby. The need for stopping an iteration in Ruby occurs when we want to interrupt the execution of the code. 

Let's discuss how to solve this problem of solving an iteration in Ruby. 

Break Statement

Break statement

In ruby, we can use the "break" statement for stopping an iteration. A break statement is the simplest way of stopping an iteration in ruby. Mostly break statement is used in the while loop. All the values are printed till the loop condition is true, or the condition for the break statement is false. 

Syntax -

If condition  

break 

Example 

i = 1
puts "Cubes till 150"
while true
    print i 
    print " Cube = " 
    puts i * i * i
	i += 1
	if i * i * i >= 150
		# Using Break Statement
		break
	end 
end
You can also try this code with Online Ruby Compiler
Run Code

Output

Output of break statement

Explanation 

In this code, we have used a break statement to check if the cube of a number is greater than 150, then no more statements will be executed further, although the condition in the while loop is still true. 
 

Although using a break statement is an efficient solution for stopping an iteration in ruby, it would not work when we want to stop the current iteration. So for that ‘Next statement’ can be used. 

Next Statement

next statement

The next statement is also used for stopping an iteration in ruby, but unlike the break statement, it will not stop the whole iteration; instead, it just stops the current iteration if the condition becomes true.

Syntax -

If condition  

next

Example

puts "Number after three"
for x in 0..6
    if x < 4 then
        # Using the next statement
next
end

# Printing values
puts "Value of x is : #{x}"
end
You can also try this code with Online Ruby Compiler
Run Code

Output

Output of next statement

Explanation 

In the above code, we have used the next statement to jump to the next iteration if the number is less than four, so the statements below the ‘next’ are not executed. As 0, 1, 2, 3 are less than 4 so for all these numbers print statement will not be executed. This will save compilation time. 

Hopefully, you have understood the solution for stopping an iteration in ruby. 

Frequently Asked Questions

What do you mean by Ruby?

Ruby is a high-level, interpreted, general-purpose programming language that supports multiple programming paradigms.

What is an iterator in Ruby?

Iterators are object-oriented concepts and are used for performing these iterations in Ruby. Iterators are supported by collections and help in returning all the elements of a collection.

What is the defining an iterator in ruby?

Syntax is collection.iterator do |variable| followed by end to close it. 

Which statement is used for stopping an iteration in Ruby?

A break statement is used for stopping an iteration in ruby. 

What is the next statement used for?

The next statement is used to skip all the rest of the current iteration statement block and jump to the next iteration statement. 

Conclusion

This blog covered the details on stopping an iteration in ruby. We learned what iteration and iterators are, how to define iterators, and what is used for stopping an iteration in ruby, along with examples.

If you want to learn more, check out our articles/blogs on ruby and refer to Ruby Documentation for more information. You can also learn more about ruby from the links given below.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, JavaScript, System Design, etc. Enroll in our courses like Competitive Programming and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Thank you

Do upvote our blog to help other ninjas grow. 

Happy Learning Ninja! 🐱‍👤

Live masterclass