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

(::int fd, ?::string | ::int mode, ?path: ::string?, ?external_encoding: ::String | ::Encoding | nil, ?internal_encoding: ::String | ::Encoding | nil, ?encoding: ::String | ::Encoding | nil, ?textmode: ::boolish, ?binmode: ::boolish, ?autoclose: ::boolish, ?mode: ::String) -> instance
[X] (::int fd, ?::string | ::int mode, ?path: ::string?, ?external_encoding: ::String | ::Encoding | nil, ?internal_encoding: ::String | ::Encoding | nil, ?encoding: ::String | ::Encoding | nil, ?textmode: ::boolish, ?binmode: ::boolish, ?autoclose: ::boolish, ?mode: ::String) { (instance) -> X } -> X
IO.open(fd, mode = 'r', **options) → io
IO.open(fd, mode = 'r', **options) {|io| ... } → object

Creates a new IO object via IO.new with the given arguments.

With no block given, returns the IO object.

With a block given, calls the block with the IO object, closes the IO object, and returns the block’s value:

Example
fd = File.sysopen('doc/maintainers.md') # => 6
IO.open(fd) {|io| io.read.size }        # => 14897
See also .new .pipe .for_fd .popen also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0