ruby - Foreach loop in XML generator not breaking -


i trying generate xml, loop isn't breaking. here part of code:

@key = 0 @cont.each |pr|   xml.product {     @key += 1     puts @key.to_s     begin       @main = nokogiri::html(open(@url+pr['href'], "user-agent" => "ruby/#{ruby_version}","from" => "foo@bar.invalid", "referer" => "http://www.ruby-lang.org/"))     rescue       puts "rescue"       next     end     puts pr['href']     puts @key.to_s     break  //this break doesn't work     #something else   } end 

most interesting in final generated xml file, break worked. file contains 1 product, on console @key printed fully, means foreach loop doesn't break.

could nokogiri xml-specific error, because of open brackets in head of loop?

in general think how you're going trying generate xml confused. don't convolute code more necessary; instead of starting generate xml aborting inside block because can't find page want, grab pages want first, then start processing.

i'd move begin/rescue block outside xml generation. existence inside xml generation block results in poor logic , questionable practices of using next , break. instead i'd recommend untested code:

@main = [] @cont.each |pr|   begin     @main << nokogiri::html(       open(@url + pr['href'])     )   rescue     puts 'rescue'     next   end end  builder = nokogiri::xml::builder.new |xml|   xml.root {     xml.products {       @main.each |m|         xml.product {           xml.id_ m.at('id').text           xml.name m.at('name').text         }       end     }   } end puts builder.to_xml 

which makes easy see code keying off being able retrieve page.

this code untested because have no idea input values or output should like. having valid input, expected output , working example of code demonstrates problem essential if want debugging problem code.

the use of @url + pr['href'] isn't idea. instead use uri class build url you. uri handles encoding , ensures uri valid.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -