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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:45:51+00:00 2026-05-31T14:45:51+00:00

What I have: Let’s say I have a hash like this, with various values

  • 0

What I have:

Let’s say I have a hash like this, with various values belonging to one parameter.

a = {}
a[:bitrate] = ["100", "500", "1000"]
a[:fps] = ["15", "30"]
a[:qp] = ["20", "30"]

What I need:

I need some way to iteratively get all the possible combinations of these values, so, with all the parameter/value pairs:

  • bitrate = 100, fps = 15, qp = 20
  • bitrate = 500, fps = 15, qp = 30
  • …

The number of parameters (i.e. the keys) and the number of values (i.e. the length of the value arrays) are not known beforehand. Ideally, I’d do something like:

a.foo do |ret|
  puts ret.keys   # => ["bitrate", "fps", "qp"]
  puts ret.values # => ["100", "15", "20"]
end

… where the block is called for each possible combination. How can I define foo?


What I (probably) don’t need:

Now, I know this: Combine array of array into all possible combinations, forward only, in Ruby, suggesting something like:

a.first.product(*a[1..-1]).map(&:join)

But this operates on values and arrays in arrays only, and I need the original reference to the parameter’s name.

  • 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-31T14:45:52+00:00Added an answer on May 31, 2026 at 2:45 pm
    a = {}
    a[:bitrate] = ["100", "500", "1000"]
    a[:fps] = ["15", "30"]
    a[:qp] = ["20", "30"]
    
    def product_hash(hsh)
      attrs   = hsh.values
      keys    = hsh.keys
      product = attrs[0].product(*attrs[1..-1])
      product.map{ |p| Hash[keys.zip p] }
    end
    
    product_hash(a)
    

    you’ll get

    [{:bitrate=>"100", :fps=>"15", :qp=>"20"},
     {:bitrate=>"100", :fps=>"15", :qp=>"30"},
     {:bitrate=>"100", :fps=>"30", :qp=>"20"},
     {:bitrate=>"100", :fps=>"30", :qp=>"30"},
     {:bitrate=>"500", :fps=>"15", :qp=>"20"},
     {:bitrate=>"500", :fps=>"15", :qp=>"30"},
     {:bitrate=>"500", :fps=>"30", :qp=>"20"},
     {:bitrate=>"500", :fps=>"30", :qp=>"30"},
     {:bitrate=>"1000", :fps=>"15", :qp=>"20"},
     {:bitrate=>"1000", :fps=>"15", :qp=>"30"},
     {:bitrate=>"1000", :fps=>"30", :qp=>"20"},
     {:bitrate=>"1000", :fps=>"30", :qp=>"30"}]
    

    You can also add new key to your hash.

    a = {}
    a[:bitrate] = ["100", "500", "1000"]
    a[:fps] = ["15", "30"]
    a[:qp] = ["20", "30"]
    a[:bw] = [true, false]
    
    product_hash(a)
    
    #=>
    [{:bitrate=>"100", :fps=>"15", :qp=>"20", :bw=>true},
     {:bitrate=>"100", :fps=>"15", :qp=>"20", :bw=>false},
     {:bitrate=>"100", :fps=>"15", :qp=>"30", :bw=>true},
     {:bitrate=>"100", :fps=>"15", :qp=>"30", :bw=>false},
     {:bitrate=>"100", :fps=>"30", :qp=>"20", :bw=>true},
     {:bitrate=>"100", :fps=>"30", :qp=>"20", :bw=>false},
     {:bitrate=>"100", :fps=>"30", :qp=>"30", :bw=>true},
     {:bitrate=>"100", :fps=>"30", :qp=>"30", :bw=>false},
     {:bitrate=>"500", :fps=>"15", :qp=>"20", :bw=>true},
     {:bitrate=>"500", :fps=>"15", :qp=>"20", :bw=>false},
     {:bitrate=>"500", :fps=>"15", :qp=>"30", :bw=>true},
     {:bitrate=>"500", :fps=>"15", :qp=>"30", :bw=>false},
     {:bitrate=>"500", :fps=>"30", :qp=>"20", :bw=>true},
     {:bitrate=>"500", :fps=>"30", :qp=>"20", :bw=>false},
     {:bitrate=>"500", :fps=>"30", :qp=>"30", :bw=>true},
     {:bitrate=>"500", :fps=>"30", :qp=>"30", :bw=>false},
     {:bitrate=>"1000", :fps=>"15", :qp=>"20", :bw=>true},
     {:bitrate=>"1000", :fps=>"15", :qp=>"20", :bw=>false},
     {:bitrate=>"1000", :fps=>"15", :qp=>"30", :bw=>true},
     {:bitrate=>"1000", :fps=>"15", :qp=>"30", :bw=>false},
     {:bitrate=>"1000", :fps=>"30", :qp=>"20", :bw=>true},
     {:bitrate=>"1000", :fps=>"30", :qp=>"20", :bw=>false},
     {:bitrate=>"1000", :fps=>"30", :qp=>"30", :bw=>true},
     {:bitrate=>"1000", :fps=>"30", :qp=>"30", :bw=>false}]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like create a custom DataRow that will have -let's say- a propery
Ok, so I have let's say this page http://mypage.com/index.php?id=something . I want to have
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a let's say ten thumb-pics of horses. And each time one horse
Let's say I have collection with 100 elements. Regular enumerator would iterate over those
I have let's say 2 (but they'll become more in the future) fully decoupled
Let's have the following class definition: CThread::CThread () { this->hThread = NULL; this->hThreadId =
I have a enum let's say enum MyEnum { FirstImage, SecondImage, ThirdImage, FourthImage };
First, if someone has a better title please help. If I have let's say
I have a let statement in which I would like to dynamically destructure 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.