Ruby | Struct to_a() function
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
animals = Struct. new ( :name , :speciality , :found_in )
detail = animals. new ( "labrador" , "bark" , "Newfoundland" )
puts detail.to_a[ 2 ]
|
Output:
Newfoundland
Example 2:
Ruby
place = Struct. new ( :name , :speciality )
detail = place. new ( "nagpur" , "orange" )
puts detail.to_a[ 1 ]
|
Output:
orange
Last Updated :
19 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...