Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
URI.open
# File tmp/rubies/ruby-4.1.0-preview1/lib/open-uri.rb, line 26
def self.open(name, *rest, &block)
if name.respond_to?(:open)
name.open(*rest, &block)
elsif name.respond_to?(:to_str) &&
%r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ name &&
(uri = URI.parse(name)).respond_to?(:open)
uri.open(*rest, &block)
else
super
end
end
Allows the opening of various resources including URIs. 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.