How should I do a loop a nokogiri search in ruby?

Posted by kim on Stack Overflow See other posts from Stack Overflow or by kim
Published on 2010-04-02T15:45:07Z Indexed on 2010/04/02 16:03 UTC
Read the original article Hit count: 211

Filed under:
|
|
|
|

I have the following that I retreive the title of each url from an array that contains a list of urls.

require 'rubygems'
require 'nokogiri'
require 'open-uri'

@urls = ["http://google.com", "http://yahoo.com", "http://rubyonrails.org"]

@found_titles = Array.new
@found_titles[0] = Nokogiri::HTML(open("#{@urls[0]}")).search("title").inner_html

#this can go on forever...but
#@found_titles[1] = Nokogiri::HTML(open("#{@urls[1]}")).search("title").inner_html
#@found_titles[2] = Nokogiri::HTML(open("#{@urls[2]}")).search("title").inner_html

puts "#{@found_titles[0]}"

How should i form a loop method for this so i can get the title even when the list in @url array gets longer.

© Stack Overflow or respective owner

Related posts about loops

Related posts about arrays