each()是Ruby中的一個內置方法,它以現有順序返回結構的每個值。如果沒有傳遞塊,它將返回一個枚舉數。
用法: struct_name.each{|x| block }
參數:該函數接受單個參數塊,這是它的迭代方式。
返回值:按其各自的順序返回每個struct成員。
例子1:
# Ruby program for each method in struct
# Include struct
Company = Struct.new(:name, :address, :zip)
#initialise struct
ele = Company.new("Geeksforgeeks", "India", 581)
# Prints the value of each member
ele.each {|x| puts(x) }
輸出:
Geeksforgeeks India 581
例子2:
# Ruby program for each method in struct
# Include struct
Employee = Struct.new(:name, :address, :zip)
#initialise struct
ele = Employee.new("Twinkle Bajaj", "India", 12345)
# Prints the value of each member
ele.each {|x| puts(x) }
輸出:
Twinkle Bajaj India 12345
相關用法
- Scala Struct ==用法及代碼示例
- Ruby Struct eql?()用法及代碼示例
- Ruby Struct filter()用法及代碼示例
- Ruby Struct members()用法及代碼示例
- Ruby Struct values_at()用法及代碼示例
- Ruby Struct to_a()用法及代碼示例
- Ruby Struct length()用法及代碼示例
- Ruby Struct size()用法及代碼示例
- Ruby Struct inspect()用法及代碼示例
- Ruby Struct values()用法及代碼示例
- Ruby Struct to_s()用法及代碼示例
- Ruby Set add?用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Ruby | Struct each() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。