Open In App

Ruby | Struct to_a() function

Last Updated : 19 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The to_a() is an inbuilt method in Ruby that returns an array with the value of the particular struct.

Syntax: struct_name.to_a[integer]

Parameters: The function accepts an integer parameter which specifies the index of the struct value to be returned.

Return Value: It returns the value of struct. 
 

Example 1:  

Ruby
# Ruby program for to_a  method in struct 
  
# Include struct
animals = Struct.new(:name, :speciality , :found_in)

#initialize values
detail = animals.new("labrador", "bark" , "Newfoundland")

#print value 2
puts detail.to_a[2]   

Output

Newfoundland


Example 2

Ruby
# Ruby program for to_a method in struct 
  
# Include struct
place = Struct.new(:name, :speciality)

#initialize values
detail = place.new("nagpur","orange")

#print value 1 
puts detail.to_a[1] 

Output

orange


 


Next Article

Similar Reads