Skip to content
Related Articles

Related Articles

Ruby | Range each() function
  • Last Updated : 18 Dec, 2019

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for each method in Range 
  
# Initialize range 
range1 = (0..10)
  
# Prints elements
puts range1.each {|el| print el, ','

chevron_right


Output:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0..10

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for each method in Range 
  
# Initialize range 
range1 = (6..12)
  
# Prints elements
puts range1.each{|el| print el, ',' }

chevron_right


Output:

6, 7, 8, 9, 10, 11, 12, 6..12
My Personal Notes arrow_drop_up
Recommended Articles
Page :