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 program for each method in Range # Initialize range range1 = (0..10) # Prints elements puts range1.each {|el| print el, ',' } |
chevron_right
filter_none
Output:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0..10
Example 2:
# Ruby program for each method in Range # Initialize range range1 = (6..12) # Prints elements puts range1.each{|el| print el, ',' } |
chevron_right
filter_none
Output:
6, 7, 8, 9, 10, 11, 12, 6..12
Recommended Posts:
- Ruby | Range new() function
- Ruby | Range first() function
- Ruby | Range end() function
- Ruby | Range last() function
- Ruby | Range eql?() function
- Ruby | Range inspect() function
- Ruby | Range member? function
- Ruby | Range begin() function
- Ruby | Range cover?() function
- Ruby | Range size() function
- Ruby | Range to_a() function
- Ruby | Range entries() function
- Ruby | Range exclude_end? function
- Ruby | Range include?() function
- Ruby | Range hash() function
- Ruby | Range to_s() function
- Ruby | Range === method
- Ruby | Range == method
- Ruby | Range Class Methods
- Ruby | pop() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

