How to use standard library?

J

Jamal Soueidan

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

Regards,
Jamal
 
S

Stefano Crocco

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

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
 
M

Michael T. Richter

--=-/QRXBY5+CcWi4cC9rOxt
Content-Type: multipart/alternative; boundary="=-+PlINj5Z+oR8cJDzaE6y"


--=-+PlINj5Z+oR8cJDzaE6y
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

I'm looking at URI class
http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/index.html
=20
The interface is somehow confusing?
=20
I tried to write
url =3D uri.new
url.parse!('http://www.test.com')
=20
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'
=3D> true
irb(main):008:0> url =3D 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.com")
=3D> #<URI::HTTP:0xfdbe7c2fe URL:http://www.booger.com>


--=20
Michael T. Richter <[email protected]> (GoogleTalk:
(e-mail address removed))
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)

--=-+PlINj5Z+oR8cJDzaE6y
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; CHARSET=3DUTF-8">
<META NAME=3D"GENERATOR" CONTENT=3D"GtkHTML/3.12.1">
</HEAD>
<BODY>
On Sun, 2007-01-04 at 20:42 +0900, Jamal Soueidan wrote:
<BLOCKQUOTE TYPE=3DCITE>
<PRE>
<FONT COLOR=3D"#000000">I'm looking at URI class</FONT>
<FONT COLOR=3D"#000000"><A HREF=3D"http://www.ruby-doc.org/stdlib/libdoc/ur=
i/rdoc/index.html">http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/index.htm=
l</A></FONT>

<FONT COLOR=3D"#000000">The interface is somehow confusing?</FONT>

<FONT COLOR=3D"#000000">I tried to write</FONT>
<FONT COLOR=3D"#000000">url =3D uri.new</FONT>
<FONT COLOR=3D"#000000">url.parse!('<A HREF=3D"http://www.test.com">http://=
www.test.com</A>')</FONT>

<FONT COLOR=3D"#000000">but this give me error that new does not exist?</FO=
NT>
</PRE>
</BLOCKQUOTE>
<BR>
To me it looks like &quot;uri&quot; doesn't exist:<BR>
<BR>
<PRE>
<TT>irb(main):007:0&gt; require 'uri'</TT>
<TT>=3D&gt; true</TT>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from (irb):8</TT>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from /usr/lib/ruby/1.8/uri/h=
ttp.rb:56</TT>
</PRE>
<BR>
Glancing at the page you posted, I see there that there is no object called=
&quot;uri&quot; anywhere.&nbsp; There is a <B>module</B> called &quot;URI&=
quot; and it has a parse method, but there isn't a class you can instantiat=
e.&nbsp; What's returned from that method (there also doesn't appear to be =
a &quot;parse!&quot; method anywhere on that page) looks like it's one of t=
he URI::&lt;something&gt; classes for each specific protocol like URI::HTTP=
and the like.<BR>
<BR>
<PRE>
<TT>irb(main):009:0&gt; URI.parse(&quot;<A HREF=3D"http://www.booger.com">h=
ttp://www.booger.com</A>&quot;)</TT>
<TT>=3D&gt; #&lt;URI::HTTP:0xfdbe7c2fe URL:<A HREF=3D"http://www.booger.com=
">http://www.booger.com</A>&gt;</TT>
</PRE>
<BR>
<TABLE CELLSPACING=3D"0" CELLPADDING=3D"0" WIDTH=3D"100%">
<TR>
<TD>
-- <BR>
<B>Michael T. Richter</B> &lt;<A HREF=3D"mailto:[email protected]">ttmri=
(e-mail address removed)</A>&gt; (<B>GoogleTalk:</B> (e-mail address removed))<BR>
<I>All really first class designers are both artists, engineers, and men of=
a powerful and intolerant temper, quick to resist the least modification o=
f the plans, energetic in fighting the least infringement upon what they re=
gard as their own sphere of action. (Nevil Shute)</I>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>

--=-+PlINj5Z+oR8cJDzaE6y--

--=-/QRXBY5+CcWi4cC9rOxt
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQBGD6WILqyWkKVQ54QRAoE2AJ9FEIHTCN0l+USPGrCLO9oY6dLDXgCeKo4J
x8Q4Jcu9Ewmuq/j3ZWGFX4o=
=hfzU
-----END PGP SIGNATURE-----

--=-/QRXBY5+CcWi4cC9rOxt--
 
S

Stefano Crocco

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
 
J

Jamal Soueidan

Stefano said:
Alle domenica 1 aprile 2007, Yamal Soueidan ha scritto:

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

Stefano Crocco

Alle domenica 1 aprile 2007, Jamal Soueidan ha scritto:
Thanks, but as I can see there is some methods there:

extract =C2=A0 join =C2=A0 parse =C2=A0 regexp =C2=A0 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 decla=
red=20
in it. The URI module knows about several kinds of URIs (from the=20
documentation, they're http, https, ftp, ldap and mailto). If the string yo=
u=20
pass to URI.parse corresponds to one of these types, then parse returns an=
=20
instance of the appropriate class. Otherwise, it will return an instance of=
=20
class URI::Generic. To see which classes are availlable, look at the sectio=
n=20
Classes and Modules under the module URI documentation. The methods you=20
mentioned (host, port) are instance methods of these classes. All the class=
es=20
used to represent URIs are derived from URI::Generic, so it's likely that=20
behaviour which doesn't depend on the kind of URI will be there. You should=
=20
look under the Methods and the Attributes sections of the documentation pag=
e=20
(for example, host and port are both listed under attributes). If you need =
to=20
use something which is specific to a kind of URI, instead, you should look =
at=20
the documentation for the class which specifically represents that kind of=
=20
URI.

I hope this helps

Stefano
 
J

Jamal Soueidan

Stefano said:
Alle domenica 1 aprile 2007, Jamal Soueidan ha scritto:

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

Jamal Soueidan

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

Gary Wright

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=91s subclasses instance from the string

The documentation is quite sparse for URI.=
 
J

Jamal Soueidan

Gary said:
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
 
J

Jamal Soueidan

Jamal said:
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?
 
S

Sebastian Hungerecker

Jamal said:
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
 
J

Jamal Soueidan

Sebastian said:
Jamal said:
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 :D

uri.host :D
 
J

James Edward Gray II

Sebastian said:
Jamal said:
* 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 :D

uri.host :D

You don't need the split() method to get this syntax:
=> "www.ruby-lang.org"

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,129
Latest member
FastBurnketo
Top