Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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
Add Comment
Please, Sign In to add comment