Created
November 15, 2020 11:02
-
-
Save Jell/6628b51914ec890350f3128d47621468 to your computer and use it in GitHub Desktop.
Fun with callcc
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
require "continuation" | |
class Hash | |
alias_method :_original_lookup, :[] | |
def [](key) | |
_original_lookup(key).tap do | |
callcc { |cc| $hash_cc = [cc]; nil }&.tap do |value_from_future| | |
self[key] = value_from_future | |
$hash_cc.pop.call(self) | |
end | |
end | |
end | |
end | |
def nil.[](key) | |
callcc { |cc| $hash_cc << cc; nil }&.tap do |value| | |
$hash_cc.pop.call(key => value) | |
end | |
end | |
def nil.[]=(key, value) | |
callcc { |cc| | |
$hash_cc.unshift(cc) | |
$hash_cc.pop.call(key => value) | |
} | |
end | |
foo = {} | |
foo["bar"]["baz"]["foo"] = 1 | |
puts foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment