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

  • Home
  • SEARCH
  • 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 6367885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:36:21+00:00 2026-05-25T00:36:21+00:00

Why do these snippets of code behave differently? I thought they were supposed to

  • 0

Why do these snippets of code behave differently? I thought they were supposed to do the same thing…

    foo = {}
    array = []

    foo['a'] = "1"
    foo['b'] = "2"

    array << foo

    foo['a'] = "3"
    foo['b'] = "4"

    array << foo

    output => [{"a"=>"3", "b"=>"4"}, {"a"=>"3", "b"=>"4"}]

This is not what I want. Fortunately, I tried using this format, which works:

    foo = {}
    array = []

    foo = {
        :a => "1",
        :b => "2"
    }

    array << foo

    foo = {
        :a => "3",
        :b => "4"
    }

    array << foo

    output => [{:a=>"1", :b=>"2"}, {:a=>"3", :b=>"4"}]

What’s going on 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-25T00:36:22+00:00Added an answer on May 25, 2026 at 12:36 am

    It’s not " vs. : — it’s that you’re modifying foo (foo[...]=...) in the first example while you’re reassigning the variable foo (foo=...) in the second. In the first example, foo still refers to the same object (which is also in the array) after you put in the values 3 & 4.

    For a solution, I recommend the second option (which you can use with '/" (strings) or : (symbols), no matter), or alternatively you can do array << foo.clone to push a copy of foo onto the array, so further modifications won’t change it.

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

Sidebar

Related Questions

It seems that these code snippets ought to behave identically: 1: Monitor.TryEnter(object) if (Monitor.TryEnter(lockObject))
The cod that I am using contains these snippets of code. I am calling
What is the difference between these code snippets? 1) $f = 12.044545; printf(%f,$f); vprintf(%f,$f);
These two code snippets produce files with different file-permissions. Example 1 creates the expected
What is the difference between these two code snippets? open (MYFILE, '>>data.txt'); open (MYFILE,
I have these 2 snippets of code that I have been playing with, but
I have some problem with these code snippets: CSS: #wrapper{ width: 600px; background: gray;
I have these two snippets of code (written in Java) which is more more
The question is, why do these code snippets give different results? private void InitializeOther()
Searching on Google reveals x2 code snippets. The first result is to this code

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.