What does this method do in Ruby?
This is actually in a Rails helper that I've seen. I see it's trying to create a hash from names, which appears to be anything that includes the Enumerable module. It's creating a hash of keys.. but where is the binding coming from? how do you pass it one? and what is happening with eval(key, binding)?
def locals_hash(names, binding)
names.inject({}) {|memo, key| memo[key.to开发者_运维知识库_sym] = eval(key, binding); memo}
end
In addition to Ken's comment, here's an example:
def locals_hash(names, binding)
names.inject({}) {|memo, key| memo[key.to_sym] = eval(key, binding); memo}
end
def m
a = 3
b = 'foo'
binding
end
locals_hash ['a', 'b'], m
#=> {:a=>3, :b=>"foo"}
精彩评论