Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8060867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:08:30+00:00 2026-06-05T10:08:30+00:00

say I’m looping through an array of products, how would I go about assigning

  • 0

say I’m looping through an array of products, how would I go about assigning a container div within that loop. For instance I want output like so:

<div class="page">
  <p>product1</p>
  <p>product2</p>
  <p>product3</p>
  <p>product4</p>
</div>
<div class="page">
  <p>product5</p>
  <p>product6</p>
  <p>product7</p>
  <p>product8</p>
</div>

I tried something like this:

<% @products.each_with_index do |product, index| %>
  <% if index%4 == 0 %>
    <div class="page">
  <%end%>
  <p><%= product.data %>
  <% if index%4 == 0 %>
    </div>>
  <%end%>
<% end %>

But as expected it will only surround every 4th product with the container. I’m thinking that I’m going to need two loops in order for this to work, but how can I do this without duplicating data?

Edit

When using alternatives to each_with_index is there any way to keep a track of the index using this method? There are also some conditional attributes that get set based on the index value.

For example:

style=<%= index == 0 ? "top:5px;" : ""%>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:08:32+00:00Added an answer on June 5, 2026 at 10:08 am

    The Rails method to split an array into groups of equal size is ary.in_groups_of(number)

    <% @products.in_groups_of(4, false) do |group| %>
      <div class="page">
        <% group.each do |product| %>
          <p><% product.data %></p>
        <% end %>
      </div>
    <% end %>
    

    Much nicer in Haml 🙂

    - @products.in_groups_of(4, false) do |group|
      .page
        - group.each do |product|
          %p= product.data
    

    You can get the absolute product index with @products.index(product) or the group index with

    <% @products.in_groups_of(4, false).each_with_index do |group, group_index| %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have 2 div s with the same CSS class that are stacked
Say I have a select box eg <div data-bind='visible: someProp'> <select class=selectSubWidgets data-bind='options: subWidgets,optionsText:
Say I have a SSI script that uses exec, or a PHP script that
Say that I have a singleton class (Downloader) responsible for downloading and persisting files.
Say I have a container with various elements inside. In turn, any element could
Say I have a LINQ-to-XML query that generates an anonymous type like this: var
Say I have a byte array with 100,000 bytes in it. I want to
Say, we have: <div class=outer> <div class=inner> <span class=text>Hello!</span> </div> </div> The 'outer' div
Say I have a higher kinded type SuperMap[Key[_],Value[_]]`. Suppose now that I had something
Say I have function with_foo that takes a block, and wrap it around a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.