Create a gist now

Instantly share code, notes, and snippets.

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