Created
May 3, 2020 06:12
-
-
Save tsaito-cyber/2e5fef2a1f0d1488775af5bbb856b244 to your computer and use it in GitHub Desktop.
future.rb
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
class Future | |
def initialize(&block) | |
@que = Queue.new | |
@t = Thread.new { | |
@que << block.call | |
} | |
end | |
def value | |
return @que.pop | |
ensure | |
@que.close | |
@t.join | |
end | |
end | |
future = Future.new do | |
sleep 3.0 | |
'done' | |
end | |
p 'ok' | |
sleep 2.0 | |
p future.value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment