Open In App

Ruby | Struct to_s() function

Last Updated : 06 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The to_s() is an inbuilt method in Ruby that returns a string with the value of the particular struct.

Syntax: struct_name.to_s()

Parameters: The function does not accepts any parameter.

Return Value: It returns the value of struct.

Example 1:




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


Output:

struct name="labrador", speciality="bark", found_in="Newfoundland"

Example 2:




# Ruby program for to_s method in struct 
    
# Include struct
animals = Struct.new(:name)
  
#initialize values
detail = animals.new("golden retriever")
  
#to_s used
puts detail.to_s


Output:

struct name="golden retriever"

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads