each()是Ruby中的內置方法,可對範圍內的每個元素進行迭代。
用法: range1.each(|el| block)
參數:該函數接受一個塊,該塊指定元素的迭代方式。
返回值:返回範圍內的每個元素。
例子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
相關用法
- Ruby Range new()用法及代碼示例
- Ruby Range last()用法及代碼示例
- Ruby Range first()用法及代碼示例
- Ruby Range eql?()用法及代碼示例
- Ruby Range end()用法及代碼示例
- Ruby Range include?()用法及代碼示例
- Ruby Range to_s()用法及代碼示例
- Ruby Range to_a()用法及代碼示例
- Ruby Range hash()用法及代碼示例
- Ruby Range member?用法及代碼示例
- Ruby Range begin()用法及代碼示例
- Ruby Range inspect()用法及代碼示例
- Ruby Range cover?()用法及代碼示例
- Ruby Range exclude_end?用法及代碼示例
- Ruby Range size()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Ruby | Range each() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。