2011-06-14 32 views
0

當我在IRB運行這些三行它工作爲什麼這段代碼讓我跳出ruby程序的循環?

date="02 Jul 2008, 08:41" 
d=Date.strptime(date, "%d %b %Y, %H:%M").iso8601 
Time.parse(d).utc.iso8601 
output=>"2008-07-01T18:30:00Z" 

但在一個循環,它在沒有這些線工作正常使用時,環路產生一個用於第一步驟的輸出後中止。 是越來越標記,其產生的時間值的代碼作爲零 示例代碼是如下

page_doc = page_doc.xpath('//div[@id="page-body"]').first 
page_doc.xpath('./div').each do |div| 
    author_node=div.xpath('.//p[@class="author"]/strong/a').first 
    if author_node == nil 
    author = "NA" 
    else 
    author = author_node.content.chomp.strip.gsub("Gjest:", "") 
    end 
    author_url_node = div.xpath('.//p[@class="author"]/strong/a/@href').first 
    if author_url_node == nil 
    author_url = "NA" 
    else 
    author_url = @site + author_url_node.content.chomp.strip 
    end 
    date_node = div.xpath('.//p[@class="author"]').first 
    date = SDF::force_ascii(date_node.content.chomp.strip.reverse[0..17].reverse) if date_node 

    content = format_description(div.xpath('.//div[@class="content"]')) 
end 
+1

對我來說,你不清楚你如何綁定你的兩個代碼示例?第二行是循環_而不是第一行?那麼你在哪裏放置線,哪些變量?或者是SDF :: force_ascii執行前4行的方法? – nathanvda 2011-06-14 09:56:00

回答

0
date_node = div.xpath('.//p[@class="author"]').first 

如果div.xpath(「.// P [@類=‘作者’]」)是空,date_node爲零。

date = SDF::force_ascii(date_node.content.chomp.strip.reverse[0..17].reverse) if date_node 
# that hurted 
date = SDF:::force_ascii(date_node.content.strip[-17, 17]) 

如果date_node是nil時,日期= SDF :: force_ascii(date_node.content.chomp.strip.reverse [0..17] .reverse)將不被執行。