Convert an object to a string with to_s. : Convert to String « String « Ruby






Convert an object to a string with to_s.

Ruby calls the to_s method from the class of the object, not the String class
parentheses are optional.

(256.0).class # => Float
(256.0).to_s # => "256.0"

 








Related examples in the same category

1.call the to_s method on many built-in classes to return the contents of the object as a string
2.change the output on the fly with the %s format flag and %: