# Passing parameter

**URL:** <https://www.ruby-forum.com/t/passing-parameter/163944>\
**Category:** Ruby\
**Created:** [March 26, 2009, 12:10pm UTC](https://www.ruby-forum.com/t/passing-parameter/163944 "2009-03-26T12:10:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [March 26, 2009, 12:10pm UTC](https://www.ruby-forum.com/t/passing-parameter/163944/1 "2009-03-26T12:10:23Z")

</div>

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 K.

---

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [March 26, 2009, 12:58pm UTC](https://www.ruby-forum.com/t/passing-parameter/163944/2 "2009-03-26T12:58:48Z")

</div>

2009/3/26 Arun K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

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

Cheers

robert

---

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [March 26, 2009, 1:01pm UTC](https://www.ruby-forum.com/t/passing-parameter/163944/3 "2009-03-26T13:01:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [March 26, 2009, 1:55pm UTC](https://www.ruby-forum.com/t/passing-parameter/163944/4 "2009-03-26T13:55:19Z")

</div>

Robert K. wrote:

> 2009/3/26 Arun K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > 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 K.

---

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [March 26, 2009, 3:37pm UTC](https://www.ruby-forum.com/t/passing-parameter/163944/5 "2009-03-26T15:37:06Z")

</div>

2009/3/26 Arun K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Robert K. 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.

Regards

robert

---

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [March 26, 2009, 4:14pm UTC](https://www.ruby-forum.com/t/passing-parameter/163944/6 "2009-03-26T16:14:37Z")

</div>

Robert K. wrote:

> 2009/3/26 Arun K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Robert K. 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\>/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 K.
