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 6164371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:58:58+00:00 2026-05-23T21:58:58+00:00

I have a factorised array of n dimensions and I would like to develop

  • 0

I have a factorised array of n dimensions and I would like to develop it.

Here is an example:

develop([:a, :aa]) #=> [[:a, :aa]]

…which is the same as: [:a].product([:aa]).

Or, more complicated:

develop([:a, [:aa, :bb]]) #=> [[:a, :aa],
                               [:a, :bb]]

I’m working with Ruby 1.9. Thank you for any idea.

Edit:

Another example, with 3 levels of embedded arrays:

develop([:a, [[:b, [:ba, :bb]],
              [:c, [:ca, :cb]],
              [:d, [:da, :db]]]]) #=> [[:a, :b, :ba],
                                       [:a, :b, :bb],
                                       [:a, :c, :ca],
                                       [:a, :c, :cb],
                                       [:a, :d, :da],
                                       [:a, :d, :db]]

I wonder if we could use Array’s product method (http://ruby-doc.org/core-1.9.3/Array.html#method-i-product), even if we have some embedded arrays.

  • 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-23T21:58:59+00:00Added an answer on May 23, 2026 at 9:58 pm

    I’m not sure I fully understand what you are trying to do to these poor arrays, but I managed to make a function that gives the correct output for both the cases you specified. Here is the complete code:

    def develop(x)
      return x unless x.is_a? Array
      y = []
      x[1].each do |s|
        d = develop(s)
        d = [d] unless d.is_a? Array
        d.each do |t|
          t = [t] unless t.is_a? Array
          y << [x.first] + t
        end
      end
      return y
    end
    
    x = [:a,
            [
                [:b, [:ba, :bb]],
                [:c, [:ca, :cb]],
                [:d, [:da, :db]]
            ]
        ]
    
    p develop(x)
    
    p develop [:a, [:aa, :bb]]
    

    The output is:

    C:\Users\David\Documents\scraps\test_ruby>ruby test.rb
    [[:a, :b, :ba], [:a, :b, :bb], [:a, :c, :ca], [:a, :c, :cb], [:a, :d, :da], [:a, :d, :db]]
    [[:a, :aa], [:a, :bb]]
    

    EDIT 1: Here’s a shorter version that also gives the right output:

    def develop(x)
      return [x] unless x.is_a? Array
      Array(x.last).collect do |s|
        develop(s).collect do |t|
          [x.first] + Array(t)
        end
      end.flatten 1
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have a rails app that references the TMY3 meteorological dataset , which is
I have a project which I want to add plugins. I have all the
I have got myself saddled with an existing application - which attempts do do
I have an application with a product detail page. On that page the user
I have a snippet to create a 'Like' button for our news site: <iframe
I have found this example on StackOverflow: var people = new List<Person> { new
I have a login.jsp page which contains a login form. Once logged in the
i have a input tag which is non editable, but some times i need
I have factories that include some setup data. For example: Factory.define :event do |event|

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.