Reputation: 45961
I'm testing this small Ruby program:
require 'net/http'
url = URI.parse('http://www.rubyinside.com/')
response = Net::HTTP.start(url.host, url.port) do |http|
http.get(url.path)
end
content = response.body
And I don't understand why URI.parse
doesn't have the prefix Net::
and Net::HTTP.start
have it.
What does Net::
means?
Upvotes: 0
Views: 36
Reputation: 33656
Net
is a module (ie. a namespace) and HTTP
is a class, so by Net::HTTP
you are accessing the HTTP
class that is namespaced under the Net
module.
::
is the namespace resolution operator, For more info see What is Ruby's double-colon (::) all about?.
As others have pointed, Net::HTTP
is designed to work closely with URI
which is another module (you could use it alone without net/http
by doing a require 'uri'
).
Thus when you require net/http
it also requires uri
, and that's the reason you can access it using URI
in your code.
Upvotes: 1
Reputation: 118299
why URI.parse doesn't have the prefix Net::
Simple Examples says that - when you would do require 'net/http'
, then This will also require ‘uri’
so you don’t need to require it separately. URI::parse is a method of the URI
module. Remember URI
and Net
are two different module.
Net
is a module. Under Net
module HTTP
class has been defined.
::
is the namespace/scope resolution operator. So to access the HTTP
class inside the module Net
,we need to use Net::HTTP
.#start
is a method of the class Net::HTTP
, that's why the call like Net::HTTP.start
.
Upvotes: 0
Reputation: 179
1) Net:: means the code is found in a module Net(namespace of ruby). It can be found in a folder net.
2) URI.parse doesn't have the prefix Net because it does not belong to Net::
Upvotes: 0