Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

IO.try_convert

(::_ToIO obj) -> ::IO
(untyped obj) -> ::IO?
IO.try_convert(object) → new_io or nil

Attempts to convert object into an IO object via method to_io; returns the new IO object if successful, or nil otherwise:

Example
IO.try_convert(STDOUT)   # => #<IO:<STDOUT>>
IO.try_convert(ARGF)     # => #<IO:<STDIN>>
IO.try_convert('STDOUT') # => nil
See also .sysopen .write .select #<< also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0