由n ruby​​迭代

时间:2012-04-22 10:23:21

标签: ruby iteration

如何创建一个不是由一个迭代而是由n?

迭代的循环

在这个例子中,我如何读取五行,下一次迭代读取其他五行,依此类推?

example = '<div id="post-editor" class="post-editor">


    <div class="wmd-container">
        <div id="wmd-button-bar" class="wmd-button-bar"></div>
        <textarea id="wmd-input" class="wmd-input" name="post-text" cols="92" rows="15" tabindex="101"></textarea>
    </div>

    <div class="fl" style="margin-top: 8px; height:24px;">&nbsp;</div>
    <div id="draft-saved" class="draft-saved community-option fl" style="margin-top: 8px; height:24px; display:none;">draft saved</div>

    <div id="draft-discarded" class="draft-discarded community-option fl" style="margin-top: 8px; height:24px; display:none;">draft discarded</div>



    <div id="wmd-preview" class="wmd-preview"></div>
    <div></div>
    <div class="edit-block">
        <input id="fkey" name="fkey" type="hidden" value="7668dcce69f925d656bc4019c29fd061">
        <input id="author" name="author" type="text">
    </div>
</div>'

puts example

1 个答案:

答案 0 :(得分:3)

你可以这样做:

example.each_line.each_slice(5) { |lines| # do something with lines }