Open In App

Ruby | Range each() function

Last Updated : 18 Dec, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The each() is an inbuilt method in Ruby iterates over every element in the range.
Syntax: range1.each(|el| block) Parameters: The function accepts a block which specifies the way in which the elements are iterated. Return Value: It returns every elements in the range.
Example 1: Ruby
# Ruby program for each method in Range 

# Initialize range 
range1 = (0..10)

# Prints elements
puts range1.each {|el| print el, ',' } 
Output:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0..10
Example 2: Ruby
# Ruby program for each method in Range 

# Initialize range 
range1 = (6..12)

# Prints elements
puts range1.each{|el| print el, ',' }
Output:
6, 7, 8, 9, 10, 11, 12, 6..12

Next Article

Similar Reads