Last active
May 7, 2019 17:47
-
-
Save sasa1977/9c43d54f6065ecaea992 to your computer and use it in GitHub Desktop.
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
cmd = ~S""" | |
ruby -e ' | |
STDOUT.sync = true | |
def receive_input | |
encoded_length = STDIN.read(4) | |
return nil unless encoded_length | |
length = encoded_length.unpack("N").first | |
STDIN.read(length) | |
end | |
def send_response(value) | |
response = value.inspect | |
STDOUT.write([response.bytesize].pack("N")) | |
STDOUT.write(response) | |
true | |
end | |
context = binding | |
while (cmd = receive_input) do | |
eval(cmd, context) | |
end | |
' | |
""" | |
port = Port.open({:spawn, cmd}, [:binary, {:packet, 4}]) | |
Port.command(port, "a = 1") | |
Port.command(port, ~S""" | |
while a < 10 do | |
a *= 3 | |
end | |
""") | |
Port.command(port, ~S""" | |
send_response("response") | |
send_response(a) | |
""") | |
receive do | |
{^port, {:data, result}} -> | |
IO.puts("Elixir got: #{inspect result}") | |
end | |
receive do | |
{^port, {:data, result}} -> | |
IO.puts("Elixir got: #{inspect result}") | |
end | |
Port.close(port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment