Skip to content

Instantly share code, notes, and snippets.

@kjwierenga
Last active August 17, 2021 12:13
Show Gist options
  • Save kjwierenga/907499 to your computer and use it in GitHub Desktop.
Save kjwierenga/907499 to your computer and use it in GitHub Desktop.
# 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