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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:19:46+00:00 2026-06-14T19:19:46+00:00

I am trying to do a little experiment which has me stumped at the

  • 0

I am trying to do a little experiment which has me stumped at the moment.

I create the new Hash

tt = Hash.new()

Then I add two hashes inside with keys:

tt.merge!(:in => Hash.new)
tt.merge!(:out => Hash.new)

So I have a hash that looks like this:

{
     :in => {},
    :out => {}
}

Now I have another hash of hashes called res that I iterate through and perform an IF statement on each one:

res.each do  |x|
    if x[:id] == nil
        tt[:out].merge!(x)
    else 
        tt[:in].merge!(x)
end 
end

However this only attaches the last value of the previous hash to both out and in inside the new hash.

What I am trying to do is use the IF statement to put new hashes under the key of IN or OUT

So it ends up looking like:

{
     :in => {{:1 => 1 ,:2 => 1 ,:3 => 1 ,:4 => 1 ,:5 => 1 },{:1 => 1 ,:2 => 1 ,:3 => 1 ,:4 => 1 ,:5 => 1 }},
    :out => {{:1 => 1 ,:2 => 1 ,:3 => 1 ,:4 => 1 ,:5 => 1 }, {:1 => 1 ,:2 => 1 ,:3 => 1 ,:4 => 1 ,:5 => 1 }}
}

Also – should I be using Hashes for this or arrays?? I want to export it eventually as JSON.

For example, this works. But not sure if it is right:

tt = Hash.new(:in => Hash.new, :out => Hash.new)
tt.merge!(:in => Array.new)
tt.merge!(:out => Array.new)
ap tt.class
res.each do  |x|
    if x[:id] == nil
        tt[:out] << x   
    else 
        tt[:in] << x
end 
end

Thnaks

  • 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-14T19:19:48+00:00Added an answer on June 14, 2026 at 7:19 pm

    This isn’t possible. You’re talking about {1,2,3,4,5} as a hash, but it’s not a hash, it’s an array. If you don’t have specific keys to associate with values, you don’t have hash-like data. Your second version which uses arrays is correct (except for your use of merge… see below).

    Also, if you want to add something to a hash, you should use the [] operator, not repeatedly use merge.

    For example, this is wrong:

    tt = Hash.new()
    tt.merge!(:in => Hash.new)
    tt.merge!(:out => Hash.new)
    

    What you want is either this:

    tt = Hash.new()
    tt[:in] = Hash.new
    tt[:out] = Hash.new
    

    or better, this:

    tt = { in: {}, out: {} }
    

    The full and correct version of this could would look something like this:

    tt = Hash.new(in: [], out: [])
    
    res.each do  |x|
      if x[:id].nil?
        tt[:out] << x   
      else 
        tt[:in] << x
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a little experiment trying to randomly place images inside a div using
I'm doing a little experiment, trying to alternate background colours for nested divs. This
I'm new to Symfony2 (with knowledge in 1.2-1.4) and having little trouble trying to
Hi i am trying out a little experiment with tricking the brain by showing
I'm trying a little experiment in haskell, wondering if it is possible to exploit
I' m trying to do a little experiment with communication between an arduino UNO
I am trying out a little reflection and have a question on how the
I am having a little trouble trying to accomplish this. Here is the gist
I'm going a little nuts trying to understand the doc on impersonation and delegation
I'm going a little nuts trying to figure out how to use template inheritance

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.