1

I have a hash:

a = { 21 => { 3 => {:x => 5, :y => 6}}}

I want to add another value to the key '21' so that the hash looks like this:

a = { 21 => { 3 => {:x => 5, :y => 6}, 4 => {:x => 8, :y => 7}}}

How can I do that?

2 Answers 2

2

You want to add an key-value pair to a hash (a[21]). a[21] will give you the inner hash object.

a = { 21 => { 3 => {:x => 5, :y => 6}}}
a[21]
# => {3=>{:x=>5, :y=>6}}

Associating key, values to the inner hash will solve your problem.

a[21][4] = {:x => 8, :y => 7}
a
# => {21=>{3=>{:x=>5, :y=>6}, 4=>{:x=>8, :y=>7}}}
Sign up to request clarification or add additional context in comments.

Comments

2

Another way is:

a[21].update({ 4=>{:x => 8, :y => 7} }) 

a #=> {21=>{3=>{:x=>5, :y=>6}, 4=>{:x=>8, :y=>7}}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.