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

IO.try_convert

IO.try_convert(obj) → io or nil

Try to convert obj into an IO, using to_io method. Returns converted IO or nil if obj cannot be converted for any reason.

Example
IO.try_convert(STDOUT)     #=> STDOUT
IO.try_convert("STDOUT")   #=> nil

require 'zlib'
f = open("/tmp/zz.gz")       #=> #<File:/tmp/zz.gz>
z = Zlib::GzipReader.open(f) #=> #<Zlib::GzipReader:0x81d8744>
IO.try_convert(z)            #=> #<File:/tmp/zz.gz>
See also .sysopen .write .select #<< also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1