Skip to content

Instantly share code, notes, and snippets.

@myobie
Last active August 29, 2015 13:58
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 myobie/9942887 to your computer and use it in GitHub Desktop.
Save myobie/9942887 to your computer and use it in GitHub Desktop.
require 'monitor'
class Promise
include MonitorMixin
NOTHING = Object.new.freeze
def initialize(&blk)
@blk = blk
@value = NOTHING
end
def value
unless value?
synchronize { work }
end
@value
end
def value?
@value !== NOTHING
end
private def work
return if value?
@value = @blk.call
@blk = nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment