Skip to content

Instantly share code, notes, and snippets.

@sasa1977
Last active May 7, 2019 17:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sasa1977/9c43d54f6065ecaea992 to your computer and use it in GitHub Desktop.
Save sasa1977/9c43d54f6065ecaea992 to your computer and use it in GitHub Desktop.
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