user181452
user181452

Reputation: 585

How to set dynamically value of nested key in Ruby hash

it should be easy, but I couldn't find a proper solution. for the first level keys:

resource.public_send("#{key}=", value)

but for foo.bar.lolo ?

I know that I can get it like the following:

'foo.bar.lolo'.split('.').inject(resource, :send)

or

resource.instance_eval("foo.bar.lolo")

but how to set the value to the last variable assuming that I don't know the nesting level, it may be second or third.

is there a general way to do that for all levels ? for my example I can do it like the following:

resource.public_send("fofo").public_send("bar").public_send("lolo=", value)

Upvotes: 4

Views: 4781

Answers (5)

Damian C. Rossney
Damian C. Rossney

Reputation: 359

Assuming that the keys are known to exist, then Hash#dig gives a cleaner solution:

hash = { a: { b: { c: 1 } } }

def deep_set(hash, value, *keys)
  hash.dig(*keys[0..-2])[keys[-1]] = value
end

deep_set(hash, 42, :a, :b, :c)
#⇒ 42

hash
#⇒ { a: { b: { c: 42 } } }

This is just example code. It will not work if the keys are not known or if deep_set receives less than two keys. Both of those issues are solvable, but beyond the OP's question.

Upvotes: 1

SMAG
SMAG

Reputation: 798

if you want to allow for initializing missing nested keys, I recommend the following refactor to Aleksei Matiushkin' solution

I didn't want to make a change to the actual answer as it is perfectly valid as is and this is introducing something extra.

hash = { a: {} } # missing some nested keys
def deep_set(hash, value, *keys)
  keys[0...-1].inject(hash) do |acc, h|
    acc[h] ||= {} # initialize the missing keys (ex: b in this case) 
    acc.public_send(:[], h)
  end.public_send(:[]=, keys.last, value)
end

deep_set(hash, 42, :a, :b, :c)
#⇒ 42
hash
#⇒ { a: { b: { c: 42 } } }

Upvotes: 0

Glyoko
Glyoko

Reputation: 2080

Although you could implement some methods to do things the way you have them set up now, I'd strongly recommend that you reconsider your data structures.

To clarify some of your terminology, the key in your example is not a key, but a method call. In Ruby, when you have code like my_thing.my_other_thing, my_other_thing is ALWAYS a method, and NEVER a key, at least not in the proper sense of the term.

It's true that you can create a hash-like structure by chaining objects in this way, but there's a real code smell to this. If you conceive of foo.bar.lolo as being a way to lookup the nested lolo key in a hash, then you should probably be using a regular hash.

x = {foo: {bar: 'lolo'}}
x[:foo][:bar] # => 'lolo'
x[:foo][:bar] = 'new_value' # => 'new_value'

Also, although the send/instance_eval methods can be used this way, it's not the best practice and can even create security problems.

Upvotes: 0

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

Answer for hashes, just out of curiosity:

hash = { a: { b: { c: 1 } } }
def deep_set(hash, value, *keys)
  keys[0...-1].inject(hash) do |acc, h|
    acc.public_send(:[], h)
  end.public_send(:[]=, keys.last, value)
end

deep_set(hash, 42, :a, :b, :c)
#⇒ 42
hash
#⇒ { a: { b: { c: 42 } } }

Upvotes: 6

max pleaner
max pleaner

Reputation: 26778

Hashes in ruby don't by default give you these dot methods.

You can chain send calls (this works on any object, but you can't access hash keys in this way normally):

  "foo".send(:downcase).send(:upcase)

When working with nested hashes the tricky concept of mutability is relevant. For example:

  hash = { a: { b: { c: 1 } } }
  nested = hash[:a][:b]
  nested[:b] = 2
  hash
  # => { a: { b: { c: 2 } }

"Mutability" here means that when you store the nested hash into a separate variable, it's still actually a pointer to the original hash. Mutability is useful for a situation like this but it can also create bugs if you don't understand it.

You can assign :a or :bto variables to make it 'dynamic' in a sense.

There are more advanced ways to do this, such as dig in newer Ruby

versions.

  hash = { a: { b: { c: 1 } } }
  keys_to_get_nested_hash = [:a, :b]
  nested_hash = hash.dig *keys_to_get_nested_hash
  nested_hash[:c] = 2
  hash
  # => { a: { b: { c: 2 } } }

If you use OpenStruct then you can give your hashes dot-method accessors. To be honest chaining send calls is not something I've used often. If it helps you write code, that's great. But you shouldn't be sending user-generated input, because it's insecure.

Upvotes: 2

Related Questions