Ruby | Struct each() function

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.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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) }

chevron_right


Output:

Geeksforgeeks
India
581

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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) }

chevron_right


Output:

Twinkle Bajaj
India
12345


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.