Ruby 连接方法调用

jes*_*era -1 ruby hash

我正在开发一个 ruby​​ 应用程序,在该应用程序中,我需要连接多个发送方法调用以访问具有键数组的复杂散列。为了使用send方法访问hash的元素,我一直在使用Hashugar gem。例如:

hash = { one: 1, two: 2, more: [{ three: 3}, {four: 4}] }.to_hashugar
keys = ['more', 'first', 'three'] # 'first' is the first elements of the array
hash.send( *keys ) # FAIL
Run Code Online (Sandbox Code Playgroud)

我一直在尝试不同的方法来使它工作,但我做不到。有没有可能实现这一目标?我是否需要删除Hashugar并不重要。

提前致谢。

Pin*_*nyM 5

你可以inject在这里使用:

keys.inject(hash){|m, key| m.send(key)}
Run Code Online (Sandbox Code Playgroud)