Skip to content

Instantly share code, notes, and snippets.

@Jell
Created November 15, 2020 11:02
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 Jell/6628b51914ec890350f3128d47621468 to your computer and use it in GitHub Desktop.
Save Jell/6628b51914ec890350f3128d47621468 to your computer and use it in GitHub Desktop.
Fun with callcc
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