Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails haml每3项新tr_Ruby On Rails_Haml - Fatal编程技术网

Ruby on rails haml每3项新tr

Ruby on rails haml每3项新tr,ruby-on-rails,haml,Ruby On Rails,Haml,我有以下代码(简化以切中要害): 我不知道如何在haml中让每4项创建一个新标记 这是它的输出方式: <tr> <tr> <th> 700.0 </th> </tr> <th> 1235.0 </th> <th> 0.8 </th> <th>

我有以下代码(简化以切中要害):

我不知道如何在haml中让每4项创建一个新标记

这是它的输出方式:

  <tr>
    <tr> 
      <th> 
        700.0
      </th> 
    </tr> 
    <th> 
      1235.0
    </th> 
    <th> 
      0.8
    </th> 
    <th> 
      650.0
    </th> 
  <tr> 
    <th> 
      1050.0
    </th> 
  </tr> 
  <th> 
    0.2
  </th> 
</tr>

700
1235
0.8
650
1050
0.2
这是我希望它的输出方式:

<tr>
  <th>
    700.0
  </th>
  <th>
    1235.0
  </th>
  <th>
    0.8
  </th>
</tr>
<tr>
  <th>
    650.0
  </th>
  <th>
    1050.0
  </th>
</tr>

700
1235
0.8
650
1050
我希望这是有意义的。

您可以使用将这些内容分为3个部分:

- @assumptions[1].each_slice(3) do |group|
  %tr
    - group.each do |ca|
      %th= ca.value

这样的答案再次证明了我喜欢堆栈溢出的原因。我刚刚将20行代码(加上一个部分)削减到8行(并删除了部分代码),这要感谢这个宝石般的答案:-)这就是函数式编程的美妙之处Ruby,我爱你。我建议你和我一起度过我的编程生涯。
- @assumptions[1].each_slice(3) do |group|
  %tr
    - group.each do |ca|
      %th= ca.value