Avatar of CHINOS
CHINOS
 asked on

ruby logic question with arrays and hashes

Hi there,  I need to manipulate a hash so that I can get a list of names with their projects.  

Right now I'm doing this in console

As you can see... in the final output there is no first project for name1 anymore, because second project overwrote the first one.  I need to append it in a way where I don't loose the first project.  

How can I do that?
>> elist = {}
=> {}
>> elist["name1"] = {:projects => ["first project"]}
=> {:projects=>["first project"]}
>> elist["name2"] = {:projects => ["first project"]}
=> {:projects=>["first project"]}
>> elist["name1"] = {:projects => ["second project"]}
=> {:projects=>["second project"]}
>> elist
=> {"name1"=>{:projects=>["second project"]}, "name2"=>{:projects=>["first project"]}}
>>

Open in new window

RubyC

Avatar of undefined
Last Comment
CHINOS

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
wesgarrison

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
CHINOS

ASKER
Thats a nice way to solve it.  I guess my mind was too much mush at the end of the day to pull out the OR EQUALS

I was actually able to do it this way too...

Thanks!
elist[id] = {:name => id, :project => []} unless elist.has_key?(id) 
elist[id][:project]  << project.data

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck