[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

passing parameter

Arun Kumar

3/26/2009 11:07:00 AM

Hi,
This is my code strip

contents.each do |content|
#scanning the content using regexp to fetch the url
url = content.scan(/(http:\/\/.*|https:\/\/.*)/i)
data = Net::HTTP.get(URI.parse("#{url}"))
#extracting the feeds url from 'data' using 'regexp'
xml = data.scan(/<link
rel="alternate".*?href="(.*?xml|.*?rdf)"/i)
end

Now what i want is to pass the 'xml' variable as a parameter to the next
class but i'm not able to do that because the 'xml' variable is inside
an array. Can anybody please tell me about how to do it.

Regards
Arun Kumar
--
Posted via http://www.ruby-....

9 Answers

Robert Klemme

3/26/2009 11:55:00 AM

0

2009/3/26 Arun Kumar <arunkumar@innovaturelabs.com>:
> Hi,
> This is my code strip
>
> =A0contents.each do |content|
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0#scanning the content using regexp=
to fetch the url
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0url =3D content.scan(/(http:\/\/.*=
|https:\/\/.*)/i)

Two notes: #scan returns a potentially empty Array and not a single object.

And you can make the regexp simpler and more efficient at the same time

/https?:\/\/.*)/i

Note also that using ".*" is dangerous because depending on your input
this may match too much.

> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0data =3D Net::HTTP.get(URI.parse("=
#{url}"))

The construct "#{expr}" is better replaced by expr.to_s and in this
case, since url should be String you do not even need that.

> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0#extracting the feeds url from 'da=
ta' using 'regexp'
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0xml =3D data.scan(/<link
> rel=3D"alternate".*?href=3D"(.*?xml|.*?rdf)"/i)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0end
>
> Now what i want is to pass the 'xml' variable as a parameter to the next
> class but i'm not able to do that because the 'xml' variable is inside
> an array. Can anybody please tell me about how to do it.

Where is "xml in an array"? The variable "xml" references an Array
because you use String#scan (see above). You can simply iterate that
Array as any other Array, too. Where is the problem?

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end

Robert Klemme

3/26/2009 11:57:00 AM

0

PS: Forgot to mention that apparently you still do not use the proper
tool for the job. Multiple have been named already (REXML, Nokogiri,
Hpricot etc.) in another thread.

--
remember.guy do |as, often| as.you_can - without end

Arun Kumar

3/26/2009 12:52:00 PM

0

Robert Klemme wrote:
> 2009/3/26 Arun Kumar <arunkumar@innovaturelabs.com>:
>> Hi,
>> This is my code strip
>>
>> �contents.each do |content|
>> � � � � � � � � � �#scanning the content using regexp to fetch the url
>> � � � � � � � � � �url = content.scan(/(http:\/\/.*|https:\/\/.*)/i)
>
> Two notes: #scan returns a potentially empty Array and not a single
> object.
>
> And you can make the regexp simpler and more efficient at the same time
>
> /https?:\/\/.*)/i
>
> Note also that using ".*" is dangerous because depending on your input
> this may match too much.
>
>> � � � � � � � � � �data = Net::HTTP.get(URI.parse("#{url}"))
>
> The construct "#{expr}" is better replaced by expr.to_s and in this
> case, since url should be String you do not even need that.
>
>> � � � � � � � � � �#extracting the feeds url from 'data' using 'regexp'
>> � � � � � � � � � �xml = data.scan(/<link
>> rel="alternate".*?href="(.*?xml|.*?rdf)"/i)
>> � � � � � � � �end
>>
>> Now what i want is to pass the 'xml' variable as a parameter to the next
>> class but i'm not able to do that because the 'xml' variable is inside
>> an array. Can anybody please tell me about how to do it.
>
> Where is "xml in an array"? The variable "xml" references an Array
> because you use String#scan (see above). You can simply iterate that
> Array as any other Array, too. Where is the problem?

Hi,
The problem is that i need to parse the xml from the feeds url and the
code for it is written in another class. So I need to pass the feed url
ie.'xml'. Since it is written inside an iterator ie. in between each
do|content|, i cannot pass it as a prameter to the other class since xml
variable will not be accessible after the iterator ends. Please help me.

N. B. Writing the code will be more helpfull for me to understand

Regards
Arun Kumar

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

Robert Klemme

3/26/2009 2:33:00 PM

0

2009/3/26 Arun Kumar <arunkumar@innovaturelabs.com>:
> Robert Klemme wrote:
>> Where is "xml in an array"? =A0The variable "xml" references an Array
>> because you use String#scan (see above). You can simply iterate that
>> Array as any other Array, too. =A0Where is the problem?
>
> The problem is that i need to parse the xml from the feeds url and the
> code for it is written in another class. So I need to pass the feed url
> ie.'xml'. Since it is written inside an iterator ie. in between each
> do|content|, i cannot pass it as a prameter to the other class since xml
> variable will not be accessible after the iterator ends. Please help me.

I don't see how the fact that variable "xml" goes out of scope at some
point in time prevents using in a different class or method. You
simply have to pass it on at the time when it is _in_ scope.

Frankly, I get the impression that you lack some basic understanding
of how Ruby works, especially what objects and variables are. I
suggest you work through some introductory material or search the
archives of this group - there have been numerous discussions about
call by reference etc.

Regards

robert

--=20
remember.guy do |as, often| as.you_can - without end

Arun Kumar

3/26/2009 3:11:00 PM

0

Robert Klemme wrote:
> 2009/3/26 Arun Kumar <arunkumar@innovaturelabs.com>:
>> Robert Klemme wrote:
>>> Where is "xml in an array"? �The variable "xml" references an Array
>>> because you use String#scan (see above). You can simply iterate that
>>> Array as any other Array, too. �Where is the problem?
>>
>> The problem is that i need to parse the xml from the feeds url and the
>> code for it is written in another class. So I need to pass the feed url
>> ie.'xml'. Since it is written inside an iterator ie. in between each
>> do|content|, i cannot pass it as a prameter to the other class since xml
>> variable will not be accessible after the iterator ends. Please help me.
>
> I don't see how the fact that variable "xml" goes out of scope at some
> point in time prevents using in a different class or method. You
> simply have to pass it on at the time when it is _in_ scope.
>
> Frankly, I get the impression that you lack some basic understanding
> of how Ruby works, especially what objects and variables are. I
> suggest you work through some introductory material or search the
> archives of this group - there have been numerous discussions about
> call by reference etc.
Hi,
Thanks for the advice. As u mentioned i'm new to ruby and i have still
to learn a lot. But the problem is that i have to prepare an assignment
in ruby regarding html extraction and to parse xml feeds from the html
content. First i have to read the url from a csv file, then i should
read the html contents of the url and then parse the xml. It is a tidy
job. I have completed a major part of it until i stuck into this part.
For providing further information I'm including the code

class hello
def hi(contents)
begin
contents.each do |content|
#scanning the content using regexp to fetch the url
url = content.scan(/(http:\/\/.*|https:\/\/.*)/i)
#getting the contents of the url using
'Net::HTTP.get' by communicating to the host server
response =
Net::HTTP.get_response(URI.parse("#{url}"))
case response
when Net::HTTPSuccess then response
when Net::HTTPRedirection then response =
Net::HTTP.get(URI.parse(response['location']))
else
response.error!
end
end
end

class extract
def parser(xmlData)
#extracting the feeds url from 'data' using 'regexp'
xmlData = response.scan(/<link
rel="alternate".*?href="(.*?xml|.*?rdf)"/i)
#rssData = data.scan(/<link rel="alternate"
type="application\/rss\+xml".*?href="(.*?)"/i)
#atomData = data.scan(/<link rel="alternate"
type="application\/atom\+xml".*?href="(.*?)"/i)

if xmlData
xmlData.each do |xml|
#getting the xml contents of the xml url
using 'Net::HTTP.get' by communicating to the host server
tags = Net::HTTP.get(URI.parse("#{xml}"))
#getting the text in between the description
tag
values =
tags.scan(/<description>(.*?)<\/description>/im)
# storing the description in a hash
details = Hash.new
details = {"#{xml}"=>"#{values}"}
end
end
end
puts "Processing complete!"
end
end

As u see the 'response' variable is the variable to be send as a
parameter to the extract class. But it is inside an
iterator(contents.each) I cannot pass the whole content to the next
class. Can u please give me a suggestion for that

Regards
Arun Kumar
--
Posted via http://www.ruby-....

Brad Greer

7/8/2011 12:12:00 AM

0

On Thu, 7 Jul 2011 04:02:39 -0700 (PDT), James Pablos
<james.pablos@gmail.com> wrote:

>On Jul 6, 11:13?pm, volkfolk <volkfo...@verizon.net> wrote:
>
>> For the record, I wouldn't work for any company that expected me to
>> take one. IMO drug tests are violation of the 4th and 5th Amendment.
>> I'm not working for anybody who starts an employer/employee
>> relationship by implying that they believe you're guilty of something
>> and implying that I'm somehow untrustworthy.
>
>You've got a million excuses, don't you? If I were you, I'd feel
>guilty about letting my wife work herself to death while I sat at home
>watching Bonanza re-runs. Seriously. I'd *would* work at McDonald's
>before I let that happen.
>
>But that's just me.

You know, fucktard doesn't begin to describe you. Have you read
anything Scot posted, or are you just off on one of your ill-informed
rants? Scot *works* more than enough - he just doesn't have a
conventional "job" in the sense of working for the same employer day
after day. I know that's tough to figure out, what with your belief
that he's not working because of drug testing, but he's said it
numerous times in this thread.

I'm sorry you couldn't handle your drugs, get over it. Plenty of
people lead very productive lives while getting high. Plenty of
industries don't test for drugs - I work in computer software, I've
never been tested and wouldn't work for an employer who tests. Scot
works construction - another industry where companies don't test.

I hope you're having a good old self-righteous time in north-central
Kentucky, or whatever shithole you call home.

Andrew

7/8/2011 12:41:00 AM

0

On Thu, 07 Jul 2011 20:12:14 -0400, Brad Greer <jjh1102us@yahoo.com>
wrote:

>On Thu, 7 Jul 2011 04:02:39 -0700 (PDT), James Pablos
><james.pablos@gmail.com> wrote:
>
>>On Jul 6, 11:13?pm, volkfolk <volkfo...@verizon.net> wrote:
>>
>>> For the record, I wouldn't work for any company that expected me to
>>> take one. IMO drug tests are violation of the 4th and 5th Amendment.
>>> I'm not working for anybody who starts an employer/employee
>>> relationship by implying that they believe you're guilty of something
>>> and implying that I'm somehow untrustworthy.
>>
>>You've got a million excuses, don't you? If I were you, I'd feel
>>guilty about letting my wife work herself to death while I sat at home
>>watching Bonanza re-runs. Seriously. I'd *would* work at McDonald's
>>before I let that happen.
>>
>>But that's just me.
>
>You know, fucktard doesn't begin to describe you. Have you read
>anything Scot posted, or are you just off on one of your ill-informed
>rants? Scot *works* more than enough - he just doesn't have a
>conventional "job" in the sense of working for the same employer day
>after day. I know that's tough to figure out, what with your belief
>that he's not working because of drug testing, but he's said it
>numerous times in this thread.
>
>I'm sorry you couldn't handle your drugs, get over it. Plenty of
>people lead very productive lives while getting high. Plenty of
>industries don't test for drugs - I work in computer software, I've
>never been tested and wouldn't work for an employer who tests. Scot
>works construction - another industry where companies don't test.
>
>I hope you're having a good old self-righteous time in north-central
>Kentucky, or whatever shithole you call home.

Heh. See, now that there's the type of thing I love reading RMGD for:
a good old-fashioned rant. As long as people like Brad Greer still
post here and start up a post every now and then with "you know,
fucktard", RMGD will always be worth reading, even if you have to wade
through a bunch of crap to get to it...

marcman

7/13/2011 1:54:00 AM

0

On Jul 4, 2:26 pm, James Pablos <james.pab...@gmail.com> wrote:
> On Jul 4, 12:29 pm, volkfolk <volkfo...@verizon.net> wrote:
>
> >The construction
> > industry in the Northeast has taken a beating. I make my living as a
> > Carpenter. It's pretty tough to find work when nobody is building
> > anything and nobody is hiring
>
> Quit with the excuses. I guarantee there is construction being done in
> your area. Get your lazy ass out there and hustle.

Oh, James . . .

james.pablos

7/13/2011 8:57:00 PM

0

On Jul 12, 9:53 pm, marcman <marcmanstud...@gmail.com> wrote:
> On Jul 4, 2:26 pm, James Pablos <james.pab...@gmail.com> wrote:

> > Quit with the excuses. I guarantee there is construction being done in
> > your area. Get your lazy ass out there and hustle.
>
> Oh, James . . .

You want to smack me, I know. :)