I have an Array of strings which represent a path of nested hash keys e.g.
["foo", "bar", "baz"]
I want to create a function which takes in the Array and a value and sets the value for the key provided in the Array, so when I call it with the example, it sets the value for
Hash["foo"]["bar"]["baz"].
Is there a method that can do that. Is there a way to chain the elements of an Array into calls for hash keys with #inject ?
Any help is highly appreciated.
To specify the question:
I have the following code:
def anonymize_data_hash(data, path=[])
if data.is_a?(Hash)
data.each do |key, value|
anonymize_data_hash(value, path + [key])
end
elsif data.is_a?(Array)
data.each do |value|
anonymize_data_hash(value, path)
end
else
path = path.clone
key = path.shift
path = (path + [data]).map(&:to_s)
send("#{key}")[path] = "xxxxxx"
save
end
end
the anonymize_data_hash method sends an method(attribute) call to a mode which is a serialized hash. Path is an array of strings. In order make the above function work I need to turn the array of string into a nested Hash call.
The Hash already exists I need to access it with the values given in the Array. Thank You for Your help.