📜  红宝石 |构造 each()函数

📅  最后修改于: 2022-05-13 01:55:14.405000             🧑  作者: Mango

红宝石 |构造 each()函数

each()是 Ruby 中的内置方法,以现有顺序返回结构的每个值。如果没有传递任何块,则返回一个枚举器。

示例 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