[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to use standard library?

Jamal Soueidan

4/1/2007 11:43:00 AM

Hello,

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

The interface is somehow confusing?

I tried to write
url = uri.new
url.parse!('http://www.tes...)

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 :)

Regards,
Jamal

--
Posted via http://www.ruby-....

15 Answers

Stefano Crocco

4/1/2007 12:07:00 PM

0

Alle domenica 1 aprile 2007, Jamal Soueidan ha scritto:
> Hello,
>
> I'm looking at URI class
> http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/...
>
> The interface is somehow confusing?
>
> I tried to write
> url = uri.new
> url.parse!('http://www.tes...)
>
> 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 :)
>
> 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.tes...)

I hope this helps

Stefano

Michael T. Richter

4/1/2007 1:46:00 PM

0

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

> I'm looking at URI class
> http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/...
>
> The interface is somehow confusing?
>
> I tried to write
> url = uri.new
> url.parse!('http://www.tes...)
>
> 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::<something> classes for each specific protocol like URI::HTTP and
the like.


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


--
Michael T. Richter <ttmrichter@gmail.com> (GoogleTalk:
ttmrichter@gmail.com)
All really first class designers are both artists, engineers, and men of
a powerful and intolerant temper, quick to resist the least modification
of the plans, energetic in fighting the least infringement upon what
they regard as their own sphere of action. (Nevil Shute)

Stefano Crocco

4/1/2007 3:26:00 PM

0

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/...), 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

Jamal Soueidan

4/1/2007 9:50:00 PM

0

Stefano Crocco 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/...), 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?

--
Posted via http://www.ruby-....

Stefano Crocco

4/1/2007 10:13:00 PM

0

Alle domenica 1 aprile 2007, Jamal Soueidan 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

Jamal Soueidan

4/1/2007 10:30:00 PM

0

Stefano Crocco wrote:
> Alle domenica 1 aprile 2007, Jamal Soueidan 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?

--
Posted via http://www.ruby-....

Jamal Soueidan

4/1/2007 10:42:00 PM

0

> 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?

--
Posted via http://www.ruby-....

Gary Wright

4/1/2007 11:07:00 PM

0


On Apr 1, 2007, at 6:42 PM, Jamal Soueidan 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 Soueidan

4/2/2007 10:24:00 AM

0

Gary Wright wrote:
> On Apr 1, 2007, at 6:42 PM, Jamal Soueidan 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


--
Posted via http://www.ruby-....

Jamal Soueidan

4/2/2007 10:49:00 AM

0

Jamal Soueidan wrote:
> Gary Wright 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?

--
Posted via http://www.ruby-....