From: Derek Cannon Date: 2010-04-19T12:15:00+09:00 Subject: Re: Elegant Solution to a Seemingly Simple Problem? >doc = Nokogiri::HTML(open(url)) > table = [] > doc.css("tr").each do |row| > cells = row.css("td").map {|cell| cell.text.strip } > next unless cells.size == 4 > next unless cells[1] == "TBA" > cells.insert(2, "") > table << cells > end This is interesting. So nil is returns for elements that activate the unless statement? > # Assuming you're using Ruby 1.9 > course_info = [] > trs = doc.css('tr') > trs.each.with_index{ |row,i| > tds = row.css('td') > title = ... > prof = ... > days = ... > times = ... > desc = ... > next_row = trs[i+1] > if next_row && next_row.is_a_continuation? > # Add content from next_row to description > # If needed, invalidate next_row so it will be skipped > elsif title && prof && days # If you have all the information you > need > course_info << Course.new( title, prof, days ) > end > } I like this code a lot, however, is it considered less efficient to look at the next row to see if it is a lab for the previous row rather than checking each row to see if it's a lab, and if it is, adding it to the last row instead? That way, every row won't need to check with the next row. -- Posted via http://www.ruby-forum.com/.