Ruby 1.9将哈希分析到一个html表中 [英] Ruby 1.9 parse hash into an html table

查看:102
本文介绍了Ruby 1.9将哈希分析到一个html表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

  {monkeys=> {hamburgers=> [爱,吃],
巨石=> [憎恨,扔]},
stonemasons=> {巨石=> [爱,使用],
vandals=> [仇恨,起诉]}
}

(我可以把任何次数的哈希内部哈希)的深度。它总是有数组作为最终值。



如何在不使用Rails的情况下将它解析为如下所示的HTML表格,最好只使用标准库? / p>

 < table> 
< tr>
< th rowspan =2>猴子< / th>
hamburgers< / th>
< td>爱< / td>
< td>吃< / td>
< / tr>
< tr>
巨石< / th>
< td>讨厌< / td>
< td>掷出< / td>
< / tr>
< tr>
th rowspan =2>石匠< / th>
巨石< / th>
< td>爱< / td>
< td>使用< / td>
< / tr>
< tr>
破坏者< / th>
< td>讨厌< / td>
< td> sue< / td>
< / tr>
< / table>


解决方案

应该这样做:

  h = {monkeys=> {hamburgers=> [爱,吃],
巨石=> [憎恨,扔]},
stonemasons=> {巨石=> [爱,使用],
vandals=> [hate,sue]}}

$ b $ def parse_data(html,data,new_line = true)

klass = data.class

#使用这个类来知道我们是否需要创建TH或TD
case
当klass == Hash
data.each do | key,value |

#开始一个新行
if new_line
html<< '< tr>'
new_line = false
end

#检查是否需要使用行数
if value.class == Array || value.count == 1
html<< <的第i;#{键}< /第> 中
else
html<< < th rowspan = \#{value.count} \>#{key}< / th>
end

#解析hash的内容(递归)
html,new_line = parse_data(html,value,new_line)
结束
当klass = Array
data.each do | item |
html<< < TD>#{项}< / TD> 中
end

#我们结束行和标志,我们需要开始一个新的
#如果有数据
html<< '< / tr>'
new_line = true
结束

返回html,new_line
结束

html ='< table> ;'
html,new_line = parse_data(html,h)
html<< '< / table>'

放入html

输出: p>

 < table> 
< tr>
< th rowspan =2>猴子< / th>
hamburgers< / th>
< td>爱< / td>
< td>吃< / td>
< / tr>
< tr>
巨石< / th>
< td>讨厌< / td>
< td>掷出< / td>
< / tr>
< tr>
th rowspan =2>石匠< / th>
巨石< / th>
< td>爱< / td>
< td>使用< / td>
< / tr>
< tr>
破坏者< / th>
< td>讨厌< / td>
< td> sue< / td>
< / tr>
< / table>


I have a Ruby hash, for example:

{"monkeys"=> {"hamburgers" => ["love", "eat"],
              "boulders" => ["hate", "throw"]},
 "stonemasons" => {"boulders" = > ["love", "use"],
                   "vandals" => ["hate", "sue"]}
}

It can have almost any level (I can put hashes inside hashes any number of times) of depth. It always has arrays as end values.

How can I parse it into an HTML table like the one below without using Rails and, preferably, using only the Standard library?

<table>
  <tr>
    <th rowspan="2">monkeys</th>
    <th>hamburgers</th>
    <td>love</td>
    <td>eat</td>
  </tr>
  <tr>
    <th>boulders</th>
    <td>hate</td>
    <td>throw</td>
  </tr>
  <tr>
    <th rowspan="2">stonemasons</th>
    <th>boulders</th>
    <td>love</td>
    <td>use</td>
  </tr>
  <tr>
    <th>vandals</th>
    <td>hate</td>
    <td>sue</td>
  </tr>
</table>

解决方案

That should do it:

h = {"monkeys"     => {"hamburgers" => ["love", "eat"],
                       "boulders"   => ["hate", "throw"]},
     "stonemasons" => {"boulders"   => ["love", "use"],
                       "vandals"    => ["hate", "sue"]}}


def parse_data(html, data, new_line = true)

  klass = data.class

  # Use the class to know if we need to create TH or TD
  case
  when klass == Hash
    data.each do |key, value|

      # Start a new row
      if new_line
        html << '<tr>'
        new_line = false
      end

      # Check if we need to use a rowspan
      if value.class == Array || value.count == 1
        html << "<th>#{key}</th>"
      else
        html << "<th rowspan=\"#{value.count}\">#{key}</th>"
      end

      # Parse the content of the hash (recursive)
      html, new_line = parse_data(html, value, new_line)
    end
  when klass = Array
    data.each do |item|
      html << "<td>#{item}</td>"
    end

    # We end the row and flag that we need to start a new one
    # if there is anymore data
    html << '</tr>'
    new_line = true
  end

  return html, new_line
end

html = '<table>'
html, new_line = parse_data(html, h)
html << '</table>'

puts html

Output:

<table>
  <tr>
    <th rowspan="2">monkeys</th>
    <th>hamburgers</th>
    <td>love</td>
    <td>eat</td>
  </tr>
  <tr>
    <th>boulders</th>
    <td>hate</td>
    <td>throw</td>
  </tr>
  <tr>
    <th rowspan="2">stonemasons</th>
    <th>boulders</th>
    <td>love</td>
    <td>use</td>
  </tr>
  <tr>
    <th>vandals</th>
    <td>hate</td>
    <td>sue</td>
  </tr>
</table>

这篇关于Ruby 1.9将哈希分析到一个html表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆