Skip to content

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

class method

URI.open

(::String name, ?::String mode, ?::Integer perm, ?untyped options) -> ((::StringIO & ::OpenURI::Meta) | (::Tempfile & ::OpenURI::Meta))
[T] (::String name, ?::String mode, ?::Integer perm, ?untyped options) { (::StringIO | ::Tempfile) -> T } -> T
open(name, *rest, &block)

Allows the opening of various resources including URIs. Example:

Example
require "open-uri"
URI.open("http://example.com") { |f| f.read }

If the first argument responds to the open method, open is called on it with the rest of the arguments.

If the first argument is a string that begins with (protocol)://, it is parsed by URI.parse. If the parsed object responds to the open method, open is called on it with the rest of the arguments.

Otherwise, Kernel#open is called.

OpenURI::OpenRead#open provides URI::HTTP#open, URI::HTTPS#open and URI::FTP#open, Kernel#open.

We can accept URIs and strings that begin with http://, https:// and ftp://. In these cases, the opened file object is extended by OpenURI::Meta.

See also .join .parse .for .parser= also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0