From: "David A. Black" Date: 2010-04-18T23:23:45+09:00 Subject: Re: Elegant Solution to a Seemingly Simple Problem? Hi -- On Sun, 18 Apr 2010, Derek Cannon wrote: >> There are lots of ways to identify more precisely which part of the HTML >> you want, using CSS selectors. Most easily, if the rows are inside >> then seomthing like 'table#courses tr' could do it. > > Since my original post, I've been playing around with the code some > more. I made a new way of getting courses that automatically filters out > "non-course" rows. The code is: > > table = doc.css("tr").collect { |row| > row.css(".dddefault").collect { |column| > column.text.strip > } > } > > This way, "non-courses" appear as empty arrays. I still don't know how > to neatly get rid of the empty arrays... I tried .compact! but that > doesn't seem to work. > >> doc = Nokogiri::HTML(open(url)) >> raw_course_list = doc.css("tr").collect { |row| >> t_row = row.css("td").collect { |column| column.text.strip } >> t_row.insert(2, "") if (t_row[1] == "TBA") >> }.reject{ |i| i.size != 4 } > > Excellent example, I think this is much better than what I had earlier. > I guess I could now replace your reject with i.empty?, right? > > PS - I changed raw_course_list to table to make it more readable. I think having the condition and the reject be the last things in the code are going to make it hard to follow it later. I wouldn't rule out doing something a tiny bit more procedural but maybe a little easier to parse visually, like this: 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 You could also extract some methods, and end up with something like: table = doc.css("tr"). select {|row| valid_row?(row) }. map {|row| prepare_row(row) } (The above is all untested.) David -- David A. Black, Senior Developer, Cyrus Innovation Inc. THE Ruby training with Black/Brown/McAnally COMPLEAT Coming to Chicago area, June 18-19, 2010! RUBYIST http://www.compleatrubyist.com