红宝石 |范围 each()函数
each()是 Ruby 中的一个内置方法,它遍历范围内的每个元素。
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.
示例 1 :
# Ruby program for each method in Range
# Initialize range
range1 = (0..10)
# Prints elements
puts range1.each {|el| print el, ',' }
输出:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0..10
示例 2 :
# Ruby program for each method in Range
# Initialize range
range1 = (6..12)
# Prints elements
puts range1.each{|el| print el, ',' }
输出:
6, 7, 8, 9, 10, 11, 12, 6..12
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。