title: Iterate Table and Replace Cell Text Under Certain Conditions
body: <p>I have a table like this in my markup:</p>
  
  <pre><code>&lt;table class="wowClassTable"&gt;
    &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Race&lt;/td&gt;
      &lt;td&gt;Faction&lt;/td&gt;
      &lt;td&gt;Link&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Human&lt;/td&gt;
      &lt;td&gt;Alliance&lt;/td&gt;
      &lt;td&gt;&lt;a href="http://www.wowwiki.com/Human_(playable)"&gt;View&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dwarf&lt;/td&gt;
      &lt;td&gt;Alliance&lt;/td&gt;
      &lt;td&gt;&lt;a href="http://www.wowwiki.com/Dwarf_(playable)"&gt;View&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Night Elf&lt;/td&gt;
      &lt;td&gt;Alliance&lt;/td&gt;
      &lt;td&gt;&lt;a href="http://www.wowwiki.com/Night_elf_(playable)"&gt;View&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>What I want to do is iterate over the table have the following array returned:</p>
  
  <pre><code>[["Race", "Faction", "Link"], ["Human", "Alliance", "View"], ["Dwarf", "Alliance", "View"], ["Night Elf", "Alliance", "View"]]
  </code></pre>
  
  <p>However I want the third element of the array to have the "View" text replaced with the actual link. So to first iterate over the table and get the array I did this:</p>
  
  <pre><code>race_table_array = Array.new
  
  rows.each do |row|
    race_array = Array.new
    row.cells.each do |cell|
      race_array &lt;&lt; cell.text
    end
    race_table_array &lt;&lt; race_array
  end
  
  puts race_table_array.inspect
  </code></pre>
  
  <p>That got me the array. So then I tried replacing the text and here I'm at a loss. I know I need to reference the link element in the cell. So I changed my row.cells iterator above to this:</p>
  
  <pre><code>row.cells.each do |cell|
    if cell.a(text: 'View').exists?
      race_array &lt;&lt; cell.a(:href)
    else
      race_array &lt;&lt; cell.text
    end
  end
  </code></pre>
  
  <p>My thinking there was I'll reference the nested 'a' (link) element inside the cell (which is a TableCell object). I'll find the element by the text 'View'. Once I have that, I'll use the href. This gives an inkling of working but is replacing my entire array so I end up with this:</p>
  
  <pre><code>[["Race", "Faction", "Link"], "http://www.wowwiki.com/Human_(playable)", "http://www.wowwiki.com/Dwarf_(playable)", "http://www.wowwiki.com/Night_elf_(playable)"]]
  </code></pre>
  
  <p>If guessing this has to do with how I'm checking if the cell has a link called 'View' but I'm not sure how to correct the problem at this point.</p>