Ruby | Struct size() function

The size() is an inbuilt method in Ruby that returns the total number of members present in struct.

Syntax: struct_name.size()

Parameters: The function does not accepts any parameter.

Return Value: It returns the integer specifying the members of struct.

Example 1:




# Ruby program for size method in struct 
    
# Include struct
Places = Struct.new(:name, :speciality)
  
#initialize values
detail = Places.new("Nagpur", "oranges")
  
# size used
puts detail.size 


Output:

2

Example 2:




# Ruby program for size method in struct 
    
# Include struct
animals = Struct.new(:name, :speciality , :found_in)
  
# initialize values
detail = animals.new("labrador", "bark" , "Newfoundland")
  
# size used
puts detail.size 


Output:

3

Contact Us