change value in hash using an array of keys in ruby -


i wondering if possible access value of hash array of keys described in post ruby use array tvalues index nested hash of hash. aim not access value change value. understood

keys.inject(hash, :fetch) 

returns value of hash-value determined key-array , not it's reference. how can accomplish modify value?

i know it's bad style modify object instead of making copy , working immutables in severel cases seems more comfortable short way.

thanks lot.

use last key nested hash, assign using last key.

keys[0...-1].inject(hash, :fetch)[keys.last] = value 

ruby doesn't have references can't reassign value directly. instead have reassign object pointer, means going 1 level of nesting.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -