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
Post a Comment