The nonlocal tells Python which variables to capture. In Ruby, you don't need a keyword like this: all variables are captured unless explicitly stated otherwise.
So, the Ruby equivalent for your Python code translates almost directly:
counter = -> { x = 0 ->y { x += y puts x } } i = counter.() i.(2)
It would probably be more idiomatic to use the method for counter , though:
def counter x = 0 ->y { x += y puts x } end i = counter i.(2)
source share