Skip to content

Commit 6f16e87

Browse files
ybiquitousmatzbot
authored andcommitted
[ruby/open-uri] Improve URI.open documentation with usage example
This improves the `URI.open` method documentation by adding a code example requiring `open-uri` as a basic usage. When reading the current documentation first, I didn't realize that `open-uri` was required to call the method. I believe the improved version could be more helpful for new users. ```sh-session $ ruby -r uri -e 'p URI.open("http://example.com")' -e:1:in '<main>': private method 'open' called for module URI (NoMethodError) ``` Ref https://docs.ruby-lang.org/en/master/URI.html#method-c-open Also, this improves formatting with code fonts for better readability. ruby/open-uri@f4400edc27
1 parent 40e3e43 commit 6f16e87

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

lib/open-uri.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,25 @@
44
require 'time'
55

66
module URI
7-
# Allows the opening of various resources including URIs.
7+
# Allows the opening of various resources including URIs. Example:
88
#
9-
# If the first argument responds to the 'open' method, 'open' is called on
9+
# require "open-uri"
10+
# URI.open("http://example.com") { |f| f.read }
11+
#
12+
# If the first argument responds to the +open+ method, +open+ is called on
1013
# it with the rest of the arguments.
1114
#
1215
# If the first argument is a string that begins with <code>(protocol)://</code>, it is parsed by
13-
# URI.parse. If the parsed object responds to the 'open' method,
14-
# 'open' is called on it with the rest of the arguments.
16+
# URI.parse. If the parsed object responds to the +open+ method,
17+
# +open+ is called on it with the rest of the arguments.
1518
#
1619
# Otherwise, Kernel#open is called.
1720
#
1821
# OpenURI::OpenRead#open provides URI::HTTP#open, URI::HTTPS#open and
1922
# URI::FTP#open, Kernel#open.
2023
#
21-
# We can accept URIs and strings that begin with http://, https:// and
22-
# ftp://. In these cases, the opened file object is extended by OpenURI::Meta.
24+
# We can accept URIs and strings that begin with <code>http://</code>, <code>https://</code> and
25+
# <code>ftp://</code>. In these cases, the opened file object is extended by OpenURI::Meta.
2326
def self.open(name, *rest, &block)
2427
if name.respond_to?(:open)
2528
name.open(*rest, &block)

0 commit comments

Comments
 (0)