Skip to content
class method

String.try_convert

(::String object) -> ::String
(::_ToStr object) -> ::String
(untyped object) -> ::String?
String.try_convert(object) → object, new_string, or nil

Attempts to convert the given object to a string.

If object is already a string, returns object, unmodified.

Otherwise if object responds to :to_str, calls object.to_str and returns the result.

Returns nil if object does not respond to :to_str.

Raises an exception unless object.to_str returns a string.

See also .new #% .json_create #* also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0