Skip to content

Instantly share code, notes, and snippets.

@jcoglan
Created August 26, 2008 15:30
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 jcoglan/7282 to your computer and use it in GitHub Desktop.
Save jcoglan/7282 to your computer and use it in GitHub Desktop.
# Allows methods to be modified so that they accept continuation-passing
# style, passing their return value to a block instead of returning it
# directly
module Continuations
METHODS = {}
def accept_continuation(*args)
args.each do |method_name|
func = instance_method(method_name)
id = func.object_id
METHODS[id] = func
module_eval <<-EOS
def #{method_name}(*params, &block)
value = Continuations::METHODS[#{id}].bind(self).call(*params)
block.call(value) if block
value
end
EOS
end
end
end
# Example
class String
extend Continuations
accept_continuation :gsub, :downcase
end
"FOO".downcase do |str|
puts str + " !!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment