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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:37:32+00:00 2026-06-14T22:37:32+00:00

I’m trying to write an Array extension that does something like this: I call

  • 0

I’m trying to write an Array extension that does something like this:

I call it like this:

%w[eggs bacon sausage].with_every_possibiity_of_multiples(2)

And it gives me these:

  [   
    %w[eggs, bacon, sausage],
    %w[eggs, eggs, bacon, sausage],
    %w[eggs, bacon, bacon, sausage],
    %w[eggs, bacon, sausage, sausage],
    %w[eggs, eggs, bacon, bacon, sausage],
    %w[eggs, eggs, bacon, bacon, sausage, sausage],
  ]

Hopefully it’s clear what’s going on here. If I had called it with an argument of 3 instead of 2, I’d have gotten an array of 9 elements, one of them being %w[eggs, eggs, eggs, bacon, sausage].

I’m struggling to come up with how to write this. Any suggestions?

  • 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-14T22:37:34+00:00Added an answer on June 14, 2026 at 10:37 pm

    As @sawa points out, you probably have your results mixed up. I’m guessing passing 2 gets you 8 elements, not 6, and passing 3 gets you 27, not 9.

    You can make good use of Array#repeated_combination to generate the number of times you want to repeat each element:

    class Array
      def with_every_possibility_of_multiples(n)
        (1..n).to_a.repeated_permutation(size).map do |repeats|
          zip(repeats).flat_map{|elem, nb| [elem] * nb}
        end
      end
    end
    
    food = %w[eggs bacon sausage]
    food.with_every_possibility_of_multiples(2) # =>
    [["eggs", "bacon", "sausage"], ["eggs", "bacon", "sausage", "sausage"], ["eggs", "bacon", "bacon", "sausage"],
     ["eggs", "bacon", "bacon", "sausage", "sausage"],
     ["eggs", "eggs", "bacon", "sausage"],
     ["eggs", "eggs", "bacon", "sausage", "sausage"],
     ["eggs", "eggs", "bacon", "bacon", "sausage"],
     ["eggs", "eggs", "bacon", "bacon", "sausage", "sausage"]]
    
    food.with_every_possibility_of_multiples(3) # =>
    [["eggs", "bacon", "sausage"],
     ["eggs", "bacon", "sausage", "sausage"],
     ["eggs", "bacon", "sausage", "sausage", "sausage"],
     ["eggs", "bacon", "bacon", "sausage"],
     ["eggs", "bacon", "bacon", "sausage", "sausage"],
     ["eggs", "bacon", "bacon", "sausage", "sausage", "sausage"],
     ["eggs", "bacon", "bacon", "bacon", "sausage"],
     ["eggs", "bacon", "bacon", "bacon", "sausage", "sausage"],
     ["eggs", "bacon", "bacon", "bacon", "sausage", "sausage", "sausage"],
     ["eggs", "eggs", "bacon", "sausage"],
     ["eggs", "eggs", "bacon", "sausage", "sausage"],
     ["eggs", "eggs", "bacon", "sausage", "sausage", "sausage"],
     ["eggs", "eggs", "bacon", "bacon", "sausage"],
     ["eggs", "eggs", "bacon", "bacon", "sausage", "sausage"],
     ["eggs", "eggs", "bacon", "bacon", "sausage", "sausage", "sausage"],
     ["eggs", "eggs", "bacon", "bacon", "bacon", "sausage"],
     ["eggs", "eggs", "bacon", "bacon", "bacon", "sausage", "sausage"],
     ["eggs", "eggs", "bacon", "bacon", "bacon", "sausage", "sausage", "sausage"],
     ["eggs", "eggs", "eggs", "bacon", "sausage"],
     ["eggs", "eggs", "eggs", "bacon", "sausage", "sausage"],
     ["eggs", "eggs", "eggs", "bacon", "sausage", "sausage", "sausage"],
     ["eggs", "eggs", "eggs", "bacon", "bacon", "sausage"],
     ["eggs", "eggs", "eggs", "bacon", "bacon", "sausage", "sausage"],
     ["eggs", "eggs", "eggs", "bacon", "bacon", "sausage", "sausage", "sausage"],
     ["eggs", "eggs", "eggs", "bacon", "bacon", "bacon", "sausage"],
     ["eggs", "eggs", "eggs", "bacon", "bacon", "bacon", "sausage", "sausage"],
     ["eggs", "eggs", "eggs", "bacon", "bacon", "bacon", "sausage", "sausage", "sausage"]]
    

    Note that repeated_permutation is new to Ruby 1.9.2. You can require 'backports' in earlier versions, or use an uglier version using product:

    # ...
    (1..n).to_a.product(*[(1..n).to_a] * (size-1)).map do |repeats|
    # ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is

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.