Skip to content

Instantly share code, notes, and snippets.

@albertoperdomo
Created April 7, 2011 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albertoperdomo/907474 to your computer and use it in GitHub Desktop.
Save albertoperdomo/907474 to your computer and use it in GitHub Desktop.
ScotRuby 2011 - Charity tutorial Excercise with bindings
def counter(start=0, increment=1)
count = nil
Proc.new do
count = count ? count+increment : start
end
end
result = counter(2,3)
puts result.call #2
puts result.call #5
puts result.call #8
puts
result = counter(2,4)
puts result.call #2
puts result.call #6
puts result.call #10
@albertoperdomo
Copy link
Author

The original solution didn't work because the count was not reset every time we call the method counter(x,y).
It's also not necessary to use instance variables because we are using the binding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment