The each() is an inbuilt method in Ruby returns every value of the struct in the existing order. In case no block is passed, it returns an enumerator.
Ruby
Output:
Ruby
Output:
Syntax: struct_name.each{|x| block } Parameters: The function accepts a single parameter block which is the way it is iterated. Return Value: It returns each struct member in its respective order.Example 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 581Example 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