2013-12-17 18 views
0

此输出10列欲把这些列我怎样才能使在红宝石所述阵列的特定列上的Rails与引入nokogiri

我确实想3 5 9

我不想显示其余的列。

使用values_at可行吗?只是不知道在哪里使用它...

def process_page 
    doc = Nokogiri::HTML(page.body) 
    doc.search('#tblResults').search('tr').map{|tr| tr.search('td').map{|t| t.text}} 
end 

<table> 
    <% @infra.each do |post| %> 
    <tr> 
    <% post.each do |t| %> 

     <td> 
     <%= t.lstrip.html_safe %> 
     </td> 
    <% end %> 
    </tr> 
<% end %> 

</table> 

回答

0
def process_page 
     doc = Nokogiri::HTML(page.body) 
    # doc.search('#tblResults').search('tr').map{|tr| tr.search('td').map{|t| t.text}} 
      doc.search('#tblResults').search('tr').map{|tr| tr.search('td').to_a.values_at(3,4,8).map{|t| t.text}} 
    end 
1

做这样的事情:

def process_page 
    doc = Nokogiri::HTML(page.body) 
    is = [ 2,4,8 ] # accounting begins from 0 
    doc.search('#tblResults').search('tr').map {|tr| tr.search('td').map.with_index {|t, i| is.include?(i) && t.text || nil }.compact } 
end 

或扩大:

def process_page 
    doc = Nokogiri::HTML(page.body) 
    is = [ 2,4,8 ] # accounting begins from 0 
    def row row 
     row.map.with_index {|t, i| is.include?(i) && t.text || nil }.compact 
    end 
    doc.search('#tblResults').search('tr').map {|tr| row tr.search('td') } 
end 
+0

哇那是一个相当长的解释...我正在与values_at鬼混,并让它工作 – gustavoanalytics