ruby - 为什么 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/方法名称冲突问题的解决方法,还是实际上有办法解决这个问题?
解决方案
您可以self.foo
明确使用
使用它会调用当前对象 self 的绑定,而不是在创建 proc/block 时关闭状态。
推荐阅读
- r - 通过添加新代码从变量中删除因式分解
- python - 如何在python中删除链表节点后立即回收它
- visual-studio - Visual Studio - 什么是建议和标准完成模式
- html - 如何清除gmail代理服务器中的缓存图像
- django - 覆盖 __str__ 允许在外键字段 Django 中传递字符串
- java - springcloud总线自定义消息无法通过rabbitmq发送
- c - 为什么用 %s 打印字符会导致分段错误?
- ruby-on-rails - Rails 无法在 docker 上启动
- javascript - HTML/Javascript addEventListener Onchange 事件在 Onchange 中?
- php - 我在 PHP 中有 2 次时间,我怎样才能运行一个 foreach 循环来度过所有这些日子?(过去 24 小时同一时间循环 1 小时!)