开发者

Why does this sequence of code gets me out of the loop in a ruby program?

When I run these three lines in irb it works

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"

but when used in a loop, which works fine without these lines, the loop discontinues after generating the output for the first step. the code which generates the date value is getting marked as nil example code is as following

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


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

if div.xpath('.//p[@class="author"]') is empty, date_node is nil.

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])

if date_node is nil, the date=SDF::force_ascii(date_node.content.chomp.strip.reverse[0..17].reverse) will not be executed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜