How to use standard library?

Hello,

I’m looking at URI class
http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/index.html

The interface is somehow confusing?

I tried to write
url = uri.new
url.parse!(‘http://www.test.com’)

but this give me error that new does not exist?
why cant I use it as a object? is the class static or is there something
i misunderstood?

In the doc they write that parse method raise URI::InvalidURIError
incase a url is incorrect typed.

when I try to use it, it doesn’t work?

begin
#code
rescue URI::InvalidURIError
p “wrong url” #this never happens even if the uri is empty?
end

Thanks for your time and effort :slight_smile:

Regards,
Jamal

Alle domenica 1 aprile 2007, Jamal S. ha scritto:

#code
rescue URI::InvalidURIError
p “wrong url” #this never happens even if the uri is empty?
end

Thanks for your time and effort :slight_smile:

Regards,
Jamal

URI is a module, not a class, so it doesn’t have a new method (you can’t
create instances of a module). I’ve never used URI, so I can’t be sure,
but,
according to the documentation, you should do:

url=URI.parse(‘http://www.test.com’)

I hope this helps

Stefano

Well, where does it identify its module and not a class?

On Sun, 2007-01-04 at 20:42 +0900, Jamal S. wrote:

I’m looking at URI class
http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/index.html

The interface is somehow confusing?

I tried to write
url = uri.new
url.parse!(‘http://www.test.com’)

but this give me error that new does not exist?

To me it looks like “uri” doesn’t exist:

irb(main):007:0> require ‘uri’
=> true
irb(main):008:0> url = uri.new
NameError: undefined local variable or method `uri’ for main:Object
from (irb):8
from /usr/lib/ruby/1.8/uri/http.rb:56

Glancing at the page you posted, I see there that there is no object
called “uri” anywhere. There is a module called “URI” and it has a
parse method, but there isn’t a class you can instantiate. What’s
returned from that method (there also doesn’t appear to be a “parse!”
method anywhere on that page) looks like it’s one of the
URI:: classes for each specific protocol like URI::HTTP and
the like.

irb(main):009:0> URI.parse(“http://www.booger.com”)
=> #<URI::HTTP:0xfdbe7c2fe URL:http://www.booger.com>

Yes I understand that its module now :slight_smile:

But how can I see its module, I can see this:

Module
URI::Escapehttp://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/Escape.html
Module
URI::REGEXPhttp://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/REGEXP.html
Class
URI::BadURIErrorhttp://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/BadURIError.html
Class
URI::Errorhttp://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/Error.html
Class
URI::FTPhttp://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/FTP.html
Class
URI::Generichttp://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/Generic.html
Class
URI::HTTPhttp://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/HTTP.html
Class
URI::HTTPShttp://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/HTTPS.html
Class
URI::InvalidComponentErrorhttp://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/InvalidComponentError.html
Class
URI::InvalidURIErrorhttp://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/InvalidURIError.html
Class
URI::LDAPhttp://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/LDAP.html
Class
URI::MailTohttp://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/MailTo.html

I thought I would use URI:HTTP ?

Another little thing is it does not throw any exceptions as mentioned in
the
doc?

Alle domenica 1 aprile 2007, Yamal Soueidan ha scritto:

Well, where does it identify its module and not a class?

If you look at the documentation page you mentioned
(http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/index.html), you see it
says: “See URI for documentation”. Clicking on the link (the work URI),
you
reach the documentation page for the URI module. There, at the left of
the
title (URI), there’s written ‘module’. This tells you URI is not a class
but
a module. An other way is to use irb:

irb: 001> require ‘uri’
true
irb: 002> URI.class
Module
irb: 003>

I hope this helps

Stefano

Stefano C. wrote:

Alle domenica 1 aprile 2007, Yamal Soueidan ha scritto:

Well, where does it identify its module and not a class?

If you look at the documentation page you mentioned
(http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/index.html), you see it
says: “See URI for documentation”. Clicking on the link (the work URI),
you
reach the documentation page for the URI module. There, at the left of
the
title (URI), there’s written ‘module’. This tells you URI is not a class
but
a module. An other way is to use irb:

irb: 001> require ‘uri’
true
irb: 002> URI.class
Module
irb: 003>

I hope this helps

Stefano

Thanks, but as I can see there is some methods there:

extract join parse regexp split

what about the attributes which I can access like

url.host or url.port ?

I don’t see they mention them on that page?

Stefano C. wrote:

Alle domenica 1 aprile 2007, Jamal S. ha scritto:

Thanks, but as I can see there is some methods there:

section
Classes and Modules under the module URI documentation. The methods you
mentioned (host, port) are instance methods of these classes. All the
classes

I hope this helps

Stefano

That was great help to understand that, but I still think the
documentation library is bad, I come from PHP world and the
documentation is very great with all sort of examples and comments from
people?

Alle domenica 1 aprile 2007, Jamal S. ha scritto:

Thanks, but as I can see there is some methods there:

extract  join  parse  regexp  split

what about the attributes which I can access like

url.host or url.port ?

I don’t see they mention them on that page?

This is because they’re not methods of the URI module, but of classes
declared
in it. The URI module knows about several kinds of URIs (from the
documentation, they’re http, https, ftp, ldap and mailto). If the string
you
pass to URI.parse corresponds to one of these types, then parse returns
an
instance of the appropriate class. Otherwise, it will return an instance
of
class URI::Generic. To see which classes are availlable, look at the
section
Classes and Modules under the module URI documentation. The methods you
mentioned (host, port) are instance methods of these classes. All the
classes
used to represent URIs are derived from URI::Generic, so it’s likely
that
behaviour which doesn’t depend on the kind of URI will be there. You
should
look under the Methods and the Attributes sections of the documentation
page
(for example, host and port are both listed under attributes). If you
need to
use something which is specific to a kind of URI, instead, you should
look at
the documentation for the class which specifically represents that kind
of
URI.

I hope this helps

Stefano

If the string you pass to URI.parse corresponds to one of these types, then
parse returns an instance of the appropriate class. Otherwise, it will return an
instance of class URI::Generic

I wonder actually how you knew that it will return one of these types
and why do you think it will return Generic?

Since I cannot find anything related to these information on the
documentation page?

On Apr 1, 2007, at 6:42 PM, Jamal S. wrote:

If the string you pass to URI.parse corresponds to one of these
types, then
parse returns an instance of the appropriate class. Otherwise, it
will return an
instance of class URI::Generic

I wonder actually how you knew that it will return one of these types
and why do you think it will return Generic?

In the description of URI.parse it says:

Creates one of the URI‘s subclasses instance from the string

The documentation is quite sparse for URI.

Jamal S. wrote:

Gary W. wrote:
I also see that split return an array of the following parts

  • Scheme
  • Userinfo
  • Host
  • Port
  • Registry
  • Path
  • Opaque
  • Query
  • Fragment

url = URI::split(url)
p url.host

This doesn’t work, host method not found?

Gary W. wrote:

On Apr 1, 2007, at 6:42 PM, Jamal S. wrote:

If the string you pass to URI.parse corresponds to one of these
types, then
parse returns an instance of the appropriate class. Otherwise, it
will return an
instance of class URI::Generic

I wonder actually how you knew that it will return one of these types
and why do you think it will return Generic?

In the description of URI.parse it says:

Creates one of the URI�s subclasses instance from the string

The documentation is quite sparse for URI.

I also see that split return an array of the following parts

  • Scheme
  • Userinfo
  • Host
  • Port
  • Registry
  • Path
  • Opaque
  • Query
  • Fragment

Jamal S. wrote:

  • Query
  • Fragment

url = URI::split(url)
p url.host

This doesn’t work, host method not found?

Well, as you said, the split method returns an array. Arrays have no
method
called host, so that’s what ruby tells you.
You’d access the host with url[2] because it’s the third element in the
array.

HTH,
Sebastian

i meant uri[:hash]

or

uri[‘hash’]

etc

Sebastian H. wrote:

Jamal S. wrote:

  • Query
  • Fragment

url = URI::split(url)
p url.host

This doesn’t work, host method not found?

Well, as you said, the split method returns an array. Arrays have no
method
called host, so that’s what ruby tells you.
You’d access the host with url[2] because it’s the third element in the
array.

HTH,
Sebastian

Ok I got that, why not use hash then if they tell us you got that in
return?

so it be more readable like this :smiley:

uri.host :smiley:

On Apr 2, 2007, at 6:19 AM, Jamal S. wrote:

Well, as you said, the split method returns an array. Arrays have no
return?

so it be more readable like this :smiley:

uri.host :smiley:

You don’t need the split() method to get this syntax:

require “uri”
=> false
uri = URI.parse(“http://www.ruby-lang.org”)
=> #<URI::HTTP:0xa77416 URL:http://www.ruby-lang.org>
uri.host
=> “www.ruby-lang.org

James Edward G. II

Thanks everyone for help :smiley:

I appropriated all the help :smiley: