Reputation: 4152
require "uri"
u = URI.parse("https://www.google.com") #<URI::HTTPS https://www.google.com>
v = URI("https://www.google.com") #<URI::HTTPS https://www.google.com>
u == v # => true
The URI.parse
is easy to understand, it calls module_function parse
on Module URI
.
How to understand URI()
in Ruby's context? What method does it called? Or is this a syntactic sugar?
Upvotes: 0
Views: 228
Reputation: 2113
Best answer on your question is source code. Code below executes when you call URI('http..')
def URI(uri)
if uri.is_a?(URI::Generic)
uri
elsif uri = String.try_convert(uri)
URI.parse(uri)
else
raise ArgumentError,
"bad argument (expected URI object or URI string)"
end
end
As you can see under the hood this method allows you to pass any argument and be sure that program will throw ArgumentError
if argument wasn't uri or instance of uri. So, in my opinion good practice to use URI('...')
than URI.parse('...')
with additional checks.
Feel free to check it out by yourself: github ruby repo mirror
Upvotes: 3