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

  • Home
  • SEARCH
  • 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 6183721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:24:51+00:00 2026-05-24T01:24:51+00:00

I have the following loop that renders an array a of arrays. Each arrays

  • 0

I have the following loop that renders an array “a” of arrays. Each arrays is defined by an index (the position of the image) and the image_id

<% a = [] %>
<% @portfolio_entry.images.each_with_index do |image, index| %>
  <% a << [index, image.id] %>
<% end %>
<%= a %>

here’s an example of the output:

[[0, 2], [1, 1], [2, 1], [3, 2], [4, 1], [5, 1], [6, 3]] 

What I want to create is a loop which can group arrays of the first three images positions, then the next three etc… in a “final” array (as my english is so-so please see the example I want to achieve:)

Finalarray => [array1, array2, array3]

array1 => [[0, 2], [1, 1], [2, 1]]        # position 0,1,2
array2 => [[3, 2], [4, 1], [5, 1]]        # position 3,4,5
array3 => [[6, 3]]                        # position 6

I tried to figure out how I can do this (collect?) but without any concrete result.

Thanks for any idea!

  • 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-05-24T01:24:52+00:00Added an answer on May 24, 2026 at 1:24 am
    a = [[0, 2], [1, 1], [2, 1], [3, 2], [4, 1], [5, 1], [6, 3]] 
    array1, array2, array3 = a.each_slice(3).to_a
    array1 # => [[0, 2], [1, 1], [2, 1]]
    array2 # => [[3, 2], [4, 1], [5, 1]]
    array3 # => [[6, 3]]
    

    Edit: if you need more arrays, leave of the to_a call and deal with the slices in the block.

    final_array = []
    a.each_slice(3) do |slice|
       final_array << slice
    end
    # or
    final_array = a.each_slice(3).inject([]) { |arr, slice| arr << slice }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following php loop that takes an array that contains a set
I have the following loop that adds an object to an array if certain
I have the following rails code in a loop that has a link for
I have the following loop which is giving me problems $(#divResults).append('<table>'); $.each( results.d, function(
I have the following loop: for(var myScreen in wizardScreens){ if(step==index)$(myScreen).show(); else $(myScreen).hide(); index++; }
Guys, I have the following code that is inside a big while loop that
I have the following loop that simply checks all green rows in a table
I have the following loop that I use several times: for(var i = 0;
I have the following code fragment that I use to scale images. This is
Right now I have a loop that loops through an array of file player

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.