# Question about ruby syntax

**URL:** <https://www.ruby-forum.com/t/question-about-ruby-syntax/171856>\
**Category:** Ruby\
**Created:** [July 10, 2009, 8:03am UTC](https://www.ruby-forum.com/t/question-about-ruby-syntax/171856 "2009-07-10T08:03:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![richardchaven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e480ec/32.png) [@richardchaven](https://www.ruby-forum.com/u/richardchaven)\
**Post date:** [July 10, 2009, 8:03am UTC](https://www.ruby-forum.com/t/question-about-ruby-syntax/171856/1 "2009-07-10T08:03:58Z")

</div>

Hi

I’ve been reading about ruby and started to learn it to replace the work  
I was doing with perl. I have a question about the code from this link.

[http://www.adaruby.com/2008/01/11/scraping-gmail-with-mechanize-and-hpricot/](http://www.adaruby.com/2008/01/11/scraping-gmail-with-mechanize-and-hpricot/)

I don’t understand this code. I can see that it is a block that returns  
array of entries containing the html for every tr with a white  
background

#################################

page.search(“//tr[@bgcolor=‘#ffffff’]”) do |row|

from, subject = _row.search(“//b/text()”)  
url = page.uri.to\_s.sub(/ui._$/,  
row.search(“//a”).first.attributes[“href”])  
puts “From: #{from}\nSubject: #{subject}\nLink: #{url}\n\n”

email = agent.get url

##################################

But what does the from, subject = \*row.search(“//b/text()”) do?

How is the \*row different than row?

Finally what does this do? I can see a regex but don’t understand the  
line.

url = page.uri.to\_s.sub(/ui.\*$/,  
row.search(“//a”).first.attributes[“href”])

Thanks in advance for your help.

Regards Richard

#################################### FULL CODE  
#############################

require ‘rubygems’  
require ‘mechanize’

agent = WWW::Mechanize.new

page = agent.get ‘[http://www.gmail.com](http://www.gmail.com)’  
form = page.forms.first  
form.Email = ‘_ **your gmail account** _’  
form.Passwd = ‘_ **your password** _’  
page = agent.submit form

page = agent.get  
page.search(“//meta”).first.attributes[‘href’].gsub(/‘/,’‘)  
page = agent.get page.uri.to\_s.sub(/?.\*$/, “?ui=html&zy=n”)  
page.search("//tr[@bgcolor=’#ffffff’]“) do |row|  
from, subject = _row.search(“//b/text()”)  
url = page.uri.to\_s.sub(/ui._$/,  
row.search(”//a").first.attributes[“href”])  
puts “From: #{from}\nSubject: #{subject}\nLink: #{url}\n\n”

email = agent.get url

# …

end

---

<div class="post-metadata">

**Author:** ![richardchaven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e480ec/32.png) [@richardchaven](https://www.ruby-forum.com/u/richardchaven)\
**Post date:** [July 10, 2009, 12:57pm UTC](https://www.ruby-forum.com/t/question-about-ruby-syntax/171856/2 "2009-07-10T12:57:32Z")

</div>

> row.search("//a").first.attributes[“href”])  
> puts “From: #{from}\nSubject: #{subject}\nLink: #{url}\n\n”
> 
> email = agent.get url
> 
> ##################################
> 
> But what does the from, subject = \*row.search("//b/text()") do?

The `*` performs what’s called a destructuring binding. This expression  
takes the array returned by `row.search("//b/text()")` and assigns one  
member to each variable listed on the left hand side. It’s equivalent  
to:

temp = row.search("//b/text()")  
from = temp[0]  
subject = temp[1]

Finally what does this do? I can see a regex but don’t understand the

> line.
> 
> url = page.uri.to\_s.sub(/ui.\*$/,  
> row.search("//a").first.attributes[“href”])

The expression `row.search("//a").first.attributes["href"]` finds the  
first  
 element in the row and gets its `href` attribute, which I imagine  
will  
be a string containing a URL. So this is basically just:

url = page.uri.to\_s.sub(/ui.\*$/, “SOME\_URL”)

So it takes the page’s URI, casts it to a string (to\_s) and replaces the  
pattern /ui.\*$/ with the `href` from an anchor tag.
