Ruby连接方法调用

时间:2015-03-17 11:55:15

标签: ruby hash

我正在开发一个ruby应用程序,我需要连接多个send方法调用以访问带有一组键的复杂哈希。为了使用send方法访问哈希的元素,我一直在使用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

我一直在尝试不同的方法让它发挥作用,但我无法做到。有没有可能实现这个目标?如果我需要删除Hashugar并不重要。

提前致谢。

1 个答案:

答案 0 :(得分:4)

您可以在此处使用inject

keys.inject(hash){|m, key| m.send(key)}