Static variables in ruby, like in C functions

ruby

Solution

Scope your variable in a method and return lambda

def counter
  count = 0
  lambda{count = count+1}
end

test = counter
test[]
#=>1
test[]
#=>2

Problem

Is there any such thing as static variables in Ruby that would behave like they do in C functions? Here's a quick example of what I mean. It prints "6\n7\n" to the console. ``` #include <stdio.h> int test() { static int a = 5; a++; return a; } int main() { printf("%d\n", test()); printf("%d\n", test()); return 0; } ```

Original source