Last active
August 17, 2021 12:13
-
-
Save kjwierenga/907499 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# My final solution to 'Closures' exercise at the | |
# Scottish Ruby Conference - Ruby Tutorial | |
# by Chad Fowler and Keavy McMinn. | |
# The returning keyword/proc is not defined in Ruby 1.8. Define it here | |
# | |
def returning(value, &block) | |
yield | |
value | |
end | |
# Use returning to remembering and returning the value | |
# of start before modifying it. | |
# | |
def counter(start, inc) | |
lambda { returning(start) { start += inc } } | |
end | |
result = counter(10, 2) | |
puts result.call | |
puts result.call | |
puts result.call | |
puts result.call | |
result = counter(2, 3) | |
puts result.call | |
puts result.call | |
puts result.call | |
puts result.call |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment