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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:25:20+00:00 2026-05-18T20:25:20+00:00

I want to duplicate a hash using the same keys but different values. I

  • 0

I want to duplicate a hash using the same keys but different values. I coded up the following snippet, and encountered something I didn’t expect:

hsh = {:foo => 'foo', :bar => 'bar'}

hsh_copy = Hash[hsh.keys.zip([[]] * hsh.length)] # => {:foo=>[], :bar=>[]}
hsh_copy[:foo] << 1
hsh_copy[:bar] << 2

hsh_copy # => {:foo=>[1, 2], :bar=>[1, 2]}

It seems that instead of copying the nested array when using the * operator, it just continues to reference the first array.

I’d be very happy if someone could explain why this is happening. Additionally, a better way of duplicating the hash would be appreciated, but I’m more concerned with understanding why * doesn’t work as expected here.

  • 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-18T20:25:21+00:00Added an answer on May 18, 2026 at 8:25 pm

    If Array#* copied the elements of the array, it would break when used on arrays with non-copyable elements (which includes, among others, numbers), which would not be desirable.

    As for how to do what you want to do: Replace hsh.keys.zip([[]] * hsh.length) with hsh.map {|k,v| [k, []] }.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to duplicate a hash using the same keys but different values. I
I want to duplicate a row, not the keys of course, without explicity using
i want to generate unique hash keys of an url. atm im using boost
Want the function to sort the table by HP but if duplicate HPs then
I want to ensure that there are no duplicate book titles in the following
I want to create wrapper class, which will enable keys duplicates while default hash
Possible Duplicate: Can PHP read the hash portion of the URL? Hi, I want
Possible Duplicate: Generate MD5 hash in Java Hi, I want to compute the MD5
I want to search a file duplicate by its hash. For performance purposes I
I have a table and I want to duplicate specific rows in the table.

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.