Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

String.try_convert

String.try_convert(object) → object, new_string, or nil

If object is a String object, returns object.

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 object.

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