Skip to content

Instantly share code, notes, and snippets.

@havenwood

havenwood/rb.rb Secret

Last active March 31, 2021 05:26
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 havenwood/74c81ec76e5a2bc1b775838e21818087 to your computer and use it in GitHub Desktop.
Save havenwood/74c81ec76e5a2bc1b775838e21818087 to your computer and use it in GitHub Desktop.
r = Ractor.new do
# Two pipes a piping...
pipes = Array.new 2 do
Ractor.new do
loop do
Ractor.yield Ractor.receive
end
end
end
loop do
# Pick a pipe at random to send to...
pipes.sample << Ractor.receive
# And select a pipe to receive from...
_pipe, response = Ractor.select(*pipes)
Ractor.yield response
end
end
r.send :test
100_000.times { r.send r.take }
r.take
#=> :test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment