From: James Coglan Date: 2009-07-10T19:56:56+09:00 Subject: Re: Question about ruby syntax --001636c5a8507995e7046e57d469 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit > 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? 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. -- James Coglan http://jcoglan.com --001636c5a8507995e7046e57d469--