What is the difference between
URI.parse? Here is what I get:
x = "http://google.com"
y = URI(x) # => #<URI::HTTP http://google.com>
z = URI.parse(x) # => #<URI::HTTP http://google.com>
y == z # => true
I see in the docs that a new instance of
URI creates a new
URI::Generic instance from generic components without check, and that it has a default parser in the args.
The general recommendation seems to be
URI.parse, and I am wondering why. I am wondering if there are any gotchas for using
URI and not using
URI.parse. Appreciate any insight.
Related: How to Parse a URL, Parse URL to Get Main Domain, Extract Host from URL string.
Best How To :
URI.parse(x) are the same.
URI is a method defined on
Kernel, which basically calls
We can confirm this by looking at the source code of the
elsif uri = String.try_convert(uri)
"bad argument (expected URI object or URI string)"
Later, in your code, the ruby parser figures out if you actually want to have a function called
URI or the module
URI depending on the syntax you use.