【发布时间】:2012-11-26 20:39:34
【问题描述】:
我正在尝试用 Ruby 编写一个闭包。这是用 Python 编写的代码:
def counter():
x = 0
def increment(y):
nonlocal x
x += y
print(x)
return increment
在 Ruby 中是否有一个“非本地”等效项,以便我可以从增量内部访问和更改变量 x?
【问题讨论】:
标签: python ruby closures python-nonlocal