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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:58:49+00:00 2026-06-17T06:58:49+00:00

I have an array. As example: a = [foo, bar, baz] I want to

  • 0

I have an array. As example:

a = ["foo", "bar", "baz"]

I want to convert it to a hash:

h = { "foo" => randomvalue1, "bar" => randomvalue2, "baz" => randomvalue3 }

I come up with following way:

Hash[* a.map { |value| [value, randomvalue_generator] }.flatten]

My experience with Ruby quite limited, but have a feeling that there should be simler way of doing this. Mainly, I am interested in reducing code complexity.

  • 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-17T06:58:51+00:00Added an answer on June 17, 2026 at 6:58 am

    Your way is fine except you do not need to flatten or splat

    a = ["foo", "bar","baz"]
    Hash[a.map{ |k| [k, rand(5)] }]
    # {"foo"=>1, "bar"=>0, "baz"=>2} 
    

    There many other ways to accomplish this but I like your original solution.

    a.each_with_object({}){ |k, h| h[k] = rand(5) }
    
    a.inject({}){ |h, k| h[k] = rand(5); h }
    
    a.inject({}){ |h, k| h.merge k => rand(5) }
    
    Hash[a.zip a.map{ |_| rand(5) }]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say, for example, I have the following array: files=( foo bar baz fizzle )
I have the array: example = ['foo', 'bar', 'quux'] I want to iterate over
I have an array with foo.bar.baz as key names in the array. Is there
I have the following array as3 example: var arrayDefinitionsargsAmfPhp:Array = new Array(); arrayDefinitionsargsAmfPhp['tabela'] =
For example, I have a String PARAMS @ FOO @ BAR @ and String
i have an array of undefined size, for example : <?php $array[foo] = 86
For example, I have this text: <content>Foo</content>This is only a <b>test</b><content>Bar</content>Again, only a test.
Given a simple zero based, numerically indexed array: var list = ['Foo', 'Bar', 'Baz'];
I have an array of homogeneous objects like so; [ { foo : bar,
I have a function returning an array: function foo(){ return array('foo'=>1,'bar'=>2); } Can I

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.