Created
April 7, 2011 10:03
-
-
Save albertoperdomo/907474 to your computer and use it in GitHub Desktop.
ScotRuby 2011 - Charity tutorial Excercise with bindings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.