Link to home
Start Free TrialLog in
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

ASKER CERTIFIED SOLUTION
Avatar of wesgarrison
wesgarrison
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CHINOS
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