首页 > 解决方案 > 为什么 instance_exec 不覆盖 Proc 对象绑定中存在的局部变量?

问题描述

我正在为一个项目编写 DSL,当我不希望局部变量渗入嵌套过程时,我遇到了一个问题。似乎无论我尝试什么,一旦将局部变量的值设置到proc的绑定中,我就无法用另一个范围内的东西覆盖它。

我正在努力工作的一个例子:

class Obj
  def foo
    :foo
  end
end

def run_proc(context, &block)
  context.instance_exec(&block)
end

def run_example(context)
  # This `foo` the local var that ends up in the binding of
  # the proc on the next line that I can't seem to overwrite
  foo = :bar   
  run_proc(context) { foo }
  # ^ I want to be able to eval `foo` based on the passed context obj
end

obj = Obj.new
# I want all three of these calls to return `:foo`
obj.foo                   #=> :foo  # works as expected
obj.instance_exec { foo } #=> :foo  # works as expected
run_example(obj)          #=> :bar  # doesn't work, since the `run_example`
                                    #  method's local `foo` var takes precedence
                                    #  over the passed object's `foo` method

我做了一些挖掘,并以与我一直在尝试的方法类似的方法找到了答案:Change the binding of a Proc in Ruby。我还研究了在 proc 的绑定中取消定义局部变量的可能性,但这个答案声称这样做是不可能的:Undefine variable in Ruby

所以我现在的问题是:我应该放弃尝试嵌套 proc 和/或找到没有本地 var/方法名称冲突问题的解决方法,还是实际上有办法解决这个问题?

标签: rubymetaprogramming

解决方案


您可以self.foo明确使用

使用它会调用当前对象 self 的绑定,而不是在创建 proc/block 时关闭状态。


推荐阅读