使用send()进行散列的setter方法

时间:2014-05-30 10:12:09

标签: ruby

Ruby中是否可以使用send方法调用哈希的setter方法? 就像我能做到的那样:

h = {a: "a", b: "b"}
h.send(:[], :a)

和GET值"a"超出此哈希值,我可以为键:a设置值吗?

1 个答案:

答案 0 :(得分:7)

是。然后该方法为[]=

示例:

hash = {:a => 1, :b => 2}
hash.send :[]=, :a, 3
hash # => {:a => 3, :b => 2}
相关问题