Skip to content
instance method

Kernel#open

(::String name, ?::String mode, ?::Integer perm) -> ::IO?
[T] (::String name, ?::String mode, ?::Integer perm) { (::IO) -> T } -> T
open(path, mode = 'r', perm = 0666, **opts) → io or nil
open(path, mode = 'r', perm = 0666, **opts) {|io| ... } → obj

Creates an IO object connected to the given file.

This method has potential security vulnerabilities if called with untrusted input; see Command Injection.

With no block given, file stream is returned:

Example
open('t.txt') # => #<File:t.txt>

With a block given, calls the block with the open file stream, then closes the stream:

Example
open('t.txt') {|f| p f } # => #<File:t.txt (closed)>

Output:

Example
#<File:t.txt>

See File.open for details.

Ruby version changes

  1. 4.0
See also #__method__ #p #loop #Pathname also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0