How to set dynamically value of nested key in Ruby hash

核能气质少年 提交于 2020-01-03 19:06:05

问题


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)

回答1:


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 } } }



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/41639364/how-to-set-dynamically-value-of-nested-key-in-ruby-hash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!